[英]Getting “NoneType object is not subscriptable” while scraping the 'aria-label' content
I am scraping a website using BeautifulSoup4 that has the content under it's aria-label
tag.我正在使用 BeautifulSoup4 抓取一个网站,该网站的内容位于
aria-label
标签下。 I have condensed down my hierarchy to a single tag but when I am trying to extract the data from it, it gives me this error: "TypeError: 'method' object is not subscriptable"
我已经将我的层次结构压缩为一个标签,但是当我试图从中提取数据时,它给了我这个错误:
"TypeError: 'method' object is not subscriptable"
A variable right_elem gives me the list of all the tags I am interested in:变量 right_elem 为我提供了我感兴趣的所有标签的列表:
right_elem = change_script[41].findAll("circle")
and the following is one of the tags I am interested:以下是我感兴趣的标签之一:
<circle aria-label="Filling capacity of the engine is 24 liters" class="graph-bullet" cx="0" cy="0" fill="#ffffff" fill-opacity="1" r="4" stroke="#ffffff" stroke-opacity="0" stroke-width="2" transform="translate(4,177)"></circle>
I have tried the following so far to overcome this issue but found no success:到目前为止,我已经尝试了以下方法来克服这个问题,但没有成功:
- right_elem[0].select_one('[class*="graph-bullet"][aria-label]')['aria-label']
- right_elem[0].find("circle", class_="graph-bullet")["aria-label"]
Any help would be appreciated.任何帮助,将不胜感激。
Try: right_elem[0].find("circle", attrs={'class': 'graph-bullet'}).attrs["aria-label"]
试试:
right_elem[0].find("circle", attrs={'class': 'graph-bullet'}).attrs["aria-label"]
Cause when you use right_elem[0].find("circle", class_="graph-bullet")
you just get HTML Parsing (Soup Object).因为当您使用
right_elem[0].find("circle", class_="graph-bullet")
时,您只会得到 HTML Parsing (Soup Object)。 And when you call .attrs
it will return a dictionary of all attributes.当您调用
.attrs
时,它将返回所有属性的字典。 Then you subscript it by ["aria-label"]
.然后用
["aria-label"]
它下标。 Hope it will work.希望它会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.