How take Content <meta content="3046 UAH" name="twitter:label1"/>

33 views
Skip to first unread message

Oleksandr Dranyi

unread,
Jun 28, 2022, 10:26:48 AM6/28/22
to beautifulsoup
# i wont take Content <meta content="3046 UAH" name="twitter:label1"/>
# if i look this :
price = soup.find_all("meta")
# i`m look meta, but if :
price = soup.find(name="twitter:label1")
NONE
#and 
price = soup.find(name="twitter:label1").get('content')
NONE

facelessuser

unread,
Jun 29, 2022, 9:43:26 PM6/29/22
to beautifulsoup
# i`m look meta, but if :
price = soup.find(name="twitter:label1")

That’s because name is already a parameter key name of the function (https://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/head:/bs4/element.py#L1871), so it won’t be recognized as an attribute name. If you wanted to search the attribute name, you’ll have to do it this way so it doesn’t conflict:

>>> soup.find(attrs={'name': 'twitter:label1'})
<meta content="3046 UAH" name="twitter:label1"/>

While it’s easier to type name="twitter:label1", I’ve personally always found this approach awkward because not all names can be specified, whether it’s class that must be done as _class or things like name which cannot be used at all. Using attrs gives you a reliable way to always get the attributes.

An alternative is to use CSS selectors:

>>> soup.select_one('meta[name="twitter:label1"]')
<meta content="3046 UAH" name="twitter:label1"/>

Oleksandr Dranyi

unread,
Jul 3, 2022, 5:37:48 PM7/3/22
to beautifulsoup
thank you very much )))

четверг, 30 июня 2022 г. в 04:43:26 UTC+3, facelessuser:
Reply all
Reply to author
Forward
0 new messages