[英]Getting TypeError: 'NoneType' object is not subscriptable in Web Scraping
[英]Getting “NoneType object is not subscriptable” while scraping the 'aria-label' content
我正在使用 BeautifulSoup4 抓取一個網站,該網站的內容位於aria-label
標簽下。 我已經將我的層次結構壓縮為一個標簽,但是當我試圖從中提取數據時,它給了我這個錯誤: "TypeError: 'method' object is not subscriptable"
變量 right_elem 為我提供了我感興趣的所有標簽的列表:
right_elem = change_script[41].findAll("circle")
以下是我感興趣的標簽之一:
<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>
到目前為止,我已經嘗試了以下方法來克服這個問題,但沒有成功:
- right_elem[0].select_one('[class*="graph-bullet"][aria-label]')['aria-label']
- right_elem[0].find("circle", class_="graph-bullet")["aria-label"]
任何幫助,將不勝感激。
試試: right_elem[0].find("circle", attrs={'class': 'graph-bullet'}).attrs["aria-label"]
因為當您使用right_elem[0].find("circle", class_="graph-bullet")
時,您只會得到 HTML Parsing (Soup Object)。 當您調用.attrs
時,它將返回所有屬性的字典。 然后用["aria-label"]
它下標。 希望它會奏效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.