我对Python很陌生,所以我很抱歉这是一个简单的问题,我正在尝试编写一个网络抓取工具,以便使用BeautifulSoup和Mechanize来搜索与网络相关的所有相关链接。
在报废时我收到的错误是:
Traceback (most recent call last):
File "scrape.py", line 30, in <module>
test.find_soup()
File "scrape.py", line 23, in find_soup
data = BS(self._launch()).read()
TypeError: 'NoneType' object is not callable
我不明白发生了什么事有人请向我解释我做错了什么?
代码:
from BeautifulSoup import BeautifulSoup as BS
import mechanize
import urlparse
class Initialize(object):
def __init__(self, url, search):
self.url = url
self.search = search
def _launch(self):
search = mechanize.Browser()
search.set_handle_robots(False)
search.set_handle_equiv(False)
search.addheaders = [('User-agent', 'Mozilla/5.0')]
search.open(self.url)
search.select_form(name="f")
search.form["q"] = self.search
return search.submit()
def find_soup(self):
data = BS(self._launch()).read()
i = 0
for link in data.select(".r a"):
return "Relevant link [{}]: {}".format(i+1,
urlparse.urlparse(link["href"]).query["q"][0])
test = Initialize("https://google.com", "networking")
test.find_soup()