2

我试图执行以下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

这个错误的原因是什么?我该如何解决这个问题?

4

1 回答 1

1

您的列表正在返回一堆值以及其中的值None

在我看来,你最好在find_all()这里使用:

for link in BeautifulSoup(response).find_all('a', href=True):
    print link['href']

href=True只会找到带有值的标签,href因此您不需要条件。

于 2013-10-17T10:20:39.193 回答