类型错误,如果link.has_attr('href“属性):类型错误:'NoneType'对象不是可调用 [英] Type error if link.has_attr('href'): TypeError: 'NoneType' object is not callable
本文介绍了类型错误,如果link.has_attr('href“属性):类型错误:'NoneType'对象不是可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图执行以下蟒蛇code
I was trying to execute the following python code
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
if link.has_attr('href'):
print link['href']
编辑:
我改变了code将该
I changed the code to this
for link in BeautifulSoup(response).find_all('a', href=True):
print link['href']
但仍然得到同样的错误。
But still getting same error
我收到错误
Traceback (most recent call last):
File "/home/user1/Documents/machinelearning/extract_links.py", line 8, in <module>
if link.has_attr('href'):
TypeError: 'NoneType' object is not callable
什么是此错误的原因是什么?
我怎样才能解决这个问题?
What is the reason for this error? How can I solve this?
推荐答案
您名单连同无
在它返回一串数值。
Your list is returning a bunch of values along with None
in it.
您最好使用 find_all()
在这里,在我看来:
You're better off using find_all()
here, in my opinion:
for link in BeautifulSoup(response).find_all('a', href=True):
print link['href']
的 HREF =真
只能找到一个的href
值的标签,这样你就不会需要你有条件的。
The href=True
will only find a tags with a href
value, so you won't need your conditional.
这篇关于类型错误,如果link.has_attr('href“属性):类型错误:'NoneType'对象不是可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文