![](/img/trans.png)
[英]web-scraping in python using beautiful soup: AttributeError: 'NoneType' object has no attribute 'text'
[英]Error nonetype object has no attribute text while scraping via beautiful soup 4 python
我正在嘗試使用 python webscraping 在 python 中使用漂亮的湯提取一些信息。 這是部分。
<div class="result-value" data-reactid=".0.0.3.0.0.3.$0.1.1">
<span data-reactid=".0.0.3.0.0.3.$0.1.1.0">751</span>
<span class="result-value-unit" data-reactid=".0.0.3.0.0.3.$0.1.1.1">KB</span>
</div
Snap: https://www.dropbox.com/s/d349tb3f22o0wyf/4.png?dl=0
我正在使用的代碼是這個
Sizeofweb=""
try:
Sizeofweb= soup.find('span', {'data-reactid': ".0.0.3.0.0.3.$0.1.1.0"}).text
print Sizeofweb
except StandardError as e:
converted_date="Error was {0}".format(e)
print converted_date
錯誤
nonetype object has no attribute text
我試過這個,但沒有用。 我哪里錯了?
這段代碼對我有用 -
from bs4 import BeautifulSoup
html_str = """
<div class="result-value" data-reactid=".0.0.3.0.0.3.$0.1.1">
<span data-reactid=".0.0.3.0.0.3.$0.1.1.0">751</span>
<span class="result-value-unit" data-reactid=".0.0.3.0.0.3.$0.1.1.1">KB</span>
</div>
"""
soup = BeautifulSoup(html_str,"lxml")
Sizeofweb = soup.find('span', {'data-reactid': ".0.0.3.0.0.3.$0.1.1.0"}).text
print Sizeofweb
輸出
751
我注意到的一件事是最后一個 div 標簽 close 缺少右尖括號 - ">"
不知道你是怎么做到的,但這對我有用......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.