我试图实现一个简单的网络爬虫,我已经写了一个简单的代码开始:有两个模块抓取器.py和爬虫.py。以下是文件:
在抓取器.py公司名称:
import urllib2
import re
def fetcher(s):
"fetch a web page from a url"
try:
req = urllib2.Request(s)
urlResponse = urllib2.urlopen(req).read()
except urllib2.URLError as e:
print e.reason
return
p,q = s.split("//")
d = q.split("/")
fdes = open(d[0],"w+")
fdes.write(str(urlResponse))
fdes.seek(0)
return fdes
if __name__ == "__main__":
defaultSeed = "http://www.python.org"
print fetcher(defaultSeed)
在爬虫.py以下内容:
^{pr2}$问题是当我跑爬虫.py对于前4-5个链接,它可以正常工作,然后会挂起,一分钟后会出现以下错误:
[Errno 110] Connection timed out
Traceback (most recent call last):
File "crawler.py", line 37, in <module>
crawler("http://www.python.org/",7)
File "crawler.py", line 34, in crawler
crawler(newSeed,n-1)
File "crawler.py", line 34, in crawler
crawler(newSeed,n-1)
File "crawler.py", line 34, in crawler
crawler(newSeed,n-1)
File "crawler.py", line 34, in crawler
crawler(newSeed,n-1)
File "crawler.py", line 34, in crawler
crawler(newSeed,n-1)
File "crawler.py", line 33, in crawler
newSeed = parse(fdes,newLinks.tell())
File "crawler.py", line 11, in parse
soup = BeautifulSoup(fd)
File "/usr/lib/python2.7/dist-packages/bs4/__init__.py", line 169, in __init__
self.builder.prepare_markup(markup, from_encoding))
File "/usr/lib/python2.7/dist-packages/bs4/builder/_lxml.py", line 68, in prepare_markup
dammit = UnicodeDammit(markup, try_encodings, is_html=True)
File "/usr/lib/python2.7/dist-packages/bs4/dammit.py", line 191, in __init__
self._detectEncoding(markup, is_html)
File "/usr/lib/python2.7/dist-packages/bs4/dammit.py", line 362, in _detectEncoding
xml_encoding_match = xml_encoding_re.match(xml_data)
TypeError: expected string or buffer
有谁能帮我这个忙吗?我对python很陌生,我不知道为什么在一段时间后连接超时?在
Connection Timeout不是python特有的,它只是表示您向服务器发出了请求,而服务器在应用程序愿意等待的时间内没有响应。在
很可能的原因是python.org网站可能有某种机制来检测它何时从一个脚本获取多个请求,并且可能在4-5个请求之后完全停止服务页面。除了在另一个站点上试用脚本之外,没有什么可以避免的。在
您可以尝试使用代理来避免在上面所述的多个请求上被检测到。您可能想看看这个答案,了解如何使用代理发送urllib请求:How to open website with urllib via Proxy - Python
相关问题 更多 >
编程相关推荐