简体   繁体   中英

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. 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"

A variable right_elem gives me the list of all the tags I am interested in:

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"]

Cause when you use right_elem[0].find("circle", class_="graph-bullet") you just get HTML Parsing (Soup Object). And when you call .attrs it will return a dictionary of all attributes. Then you subscript it by ["aria-label"] . Hope it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM