BeautifulSoup .text Python中的AttributeError

时间:2016-12-07 18:34:19

标签: python beautifulsoup urllib2

我使用BeautifulSoup很长一段时间,现在我写的每个旧的或新的脚本都显示出这个错误,但这应该有用。

title = soup.find('span',{"class" : 'product-name'}).text.encode(utf-8)

即使我尝试这样的事情:

title = soup.find('span', 'product-name').text.encode(utf-8)

我不知道为什么会发生这种情况,这是正确的。我也运行过去运行的旧脚本,但又出现了AttributeError异常的问题。有人有什么建议吗?

编辑:

我尝试过与selenium类似的脚本,并且它正在运行。它支持.text属性。我也尝试用.get_text()替换.text并再次使用相同的错误。也没有识别lxml的导入(避免在回溯开始时启动BeautifulSoup警告)

回溯:

Warning (from warnings module):
  File "C:\Python27\lib\site-packages\bs4\__init__.py", line 181
    markup_type=markup_type))
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 1 of the file <string>. To get rid of this warning, change code that looks like this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")


Traceback (most recent call last):
  File "C:\Users\Uros\Desktop\Products-script\Test03.py", line 31, in <module>
    title = soup.find('span',{"class" : 'product-name'}).get_text().encode(utf-8)
AttributeError: 'NoneType' object has no attribute 'get_text'

0 个答案:

没有答案