【发布时间】:2013-10-17 10:18:19
【问题描述】:
我试图执行以下 python 代码
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']
编辑: 我把代码改成了这个
for link in BeautifulSoup(response).find_all('a', href=True):
print link['href']
但还是报同样的错误
我收到了错误
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
这个错误的原因是什么? 我该如何解决这个问题?
【问题讨论】:
标签: python beautifulsoup