beautifulsoup获取属性_Python:BeautifulSoup-根据名称属性获取属性值

Python:BeautifulSoup-根据名称属性获取属性值

我想根据属性名称打印属性值,例如

我想做这样的事情

soup = BeautifulSoup(f) //f is some HTML containing the above meta tag

for meta_tag in soup('meta'):

if meta_tag['name'] == 'City':

print meta_tag['content']

上面的代码给出了KeyError: 'name',我相信这是因为BeatifulSoup使用了name,因此它不能用作关键字参数。

Ruth asked 2019-09-24T03:39:54Z

6个解决方案

121 votes

这很简单,请使用以下代码-

>>> from bs4 import BeautifulSoup

>>> soup = BeautifulSoup('')

>>> soup.find("meta", {"name":"City"})

>>> soup.find("meta", {"name":"City"})['content']

u'Austin'

如果不清楚,请发表评论。

theharshest answered 2019-09-24T03:40:14Z

22 votes

最严厉的回答了这个问题,但这是做同样事情的另一种方法。另外,在您的示例中,大写字母为NAME,在代码中的字母为小写。

s = '

Hello World
'

soup = BeautifulSoup(s)

attributes_dictionary = soup.find('div').attrs

print attributes_dictionary

# prints: {'id': 'get attrs', 'x': 'something', 'class': ['question'], 'name': 'python'}

print attributes_dictionary['class'][0]

# prints: question

print soup.find('div').get_text()

# prints: Hello World

Delicious answered 2019-09-24T03:40:38Z

8 votes

以下作品:

from bs4 import BeautifulSoup

soup = BeautifulSoup('', 'html.parser')

metas = soup.find_all("meta")

for meta in metas:

print meta.attrs['content'], meta.attrs['name']

BrightMoon answered 2019-09-24T03:41:02Z

7 votes

最棘手的答案是最好的解决方案,但仅供参考,您遇到的问题与“ Beautiful Soup”中的Tag对象像Python字典一样起作用。 如果访问不具有“名称”属性的标签上的tag ['name'],则会收到KeyError。

Leonard Richardson answered 2019-09-24T03:41:26Z

4 votes

晚了6年,但我一直在寻找如何提取html元素的标签属性值的方法,因此:

Ayr

我想要“ addressLocality”。 我一直被引导回到这里,但是答案并没有真正解决我的问题。

我最终如何做到的:

>>> from bs4 import BeautifulSoup as bs

>>> soup = bs('Ayr', 'html.parser')

>>> my_attributes = soup.find().attrs

>>> my_attributes

{u'property': u'addressLocality'}

这是命令,因此您还可以使用keys和“值”

>>> my_attributes.keys()

[u'property']

>>> my_attributes.values()

[u'addressLocality']

希望它可以帮助其他人!

ron g answered 2019-09-24T03:42:17Z

0 votes

也可以尝试以下解决方案:

查找值,该值写在表的跨度中

htmlContent

ID

Name

ID123

Bonny

Python代码

soup = BeautifulSoup(htmlContent, "lxml")

soup.prettify()

tables = soup.find_all("table")

for table in tables:

storeValueRows = table.find_all("tr")

thValue = storeValueRows[0].find_all("th")[0].string

if (thValue == "ID"): # with this condition I am verifying that this html is correct, that I wanted.

value = storeValueRows[1].find_all("span")[0].string

value = value.strip()

# storeValueRows[1] will represent

tag of table located at first index and find_all("span")[0] will give me tag and '.string' will give me value

# value.strip() - will remove space from start and end of the string.

# find using attribute :

value = storeValueRows[1].find("span", {"name":"spanId"})['class']

print value

# this will print spanclass

Ujjaval Moradiya answered 2019-09-24T03:42:56Z

你可能感兴趣的:(beautifulsoup获取属性_Python:BeautifulSoup-根据名称属性获取属性值)