[英]Error nonetype object has no attribute text while scraping via beautiful soup 4 python
I am trying to extract some info using beautiful soup in python using python webscraping.我正在尝试使用 python webscraping 在 python 中使用漂亮的汤提取一些信息。 Here is the section.
这是部分。
<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
Code I am using is this我正在使用的代码是这个
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
Error错误
nonetype object has no attribute text
I have tried this but didnt work.我试过这个,但没有用。 Where am I wrong?
我哪里错了?
This code works for me -这段代码对我有用 -
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
Output输出
751
One thing I noticed is the last div tag close is missing the close angle bracket - ">"我注意到的一件事是最后一个 div 标签 close 缺少右尖括号 - ">"
Dunno how you've done it but this works for me...不知道你是怎么做到的,但这对我有用......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.