首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python爬虫:连接超时

Python爬虫:连接超时
EN

Stack Overflow用户
提问于 2013-01-23 22:58:38
回答 2查看 1.9K关注 0票数 0

我正在尝试实现一个简单的web爬虫,我已经编写了一个简单的代码来开始:有两个模块fetcher.pycrawler.py。以下是这些文件:

fetcher.py:

代码语言:javascript
复制
    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)

crawler.py:

代码语言:javascript
复制
from bs4 import BeautifulSoup
import re
from fetchpage import fetcher    

usedLinks = open("Used","a+")
newLinks = open("New","w+")

newLinks.seek(0)

def parse(fd,var=0):
        soup = BeautifulSoup(fd)
        for li in soup.find_all("a",href=re.compile("http")):
                newLinks.seek(0,2)
                newLinks.write(str(li.get("href")).strip("/"))
                newLinks.write("\n")

        fd.close()
        newLinks.seek(var)
        link = newLinks.readline().strip("\n")

        return str(link)


def crawler(seed,n):
        if n == 0:
                usedLinks.close()
                newLinks.close()
                return
        else:
                usedLinks.write(seed)
                usedLinks.write("\n")
                fdes = fetcher(seed)
                newSeed = parse(fdes,newLinks.tell())
                crawler(newSeed,n-1)

if __name__ == "__main__":
        crawler("http://www.python.org/",7)

问题是,当我运行crawler.py时,它对前4-5链接很好地工作,然后挂起,一分钟后给出以下错误:

代码语言:javascript
复制
[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非常陌生,我也不知道为什么说连接超时过了一段时间?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-24 01:12:32

连接超时并不是特定于python的,它只是意味着您向服务器发出了请求,并且服务器没有在应用程序愿意等待的时间内做出响应。

很可能发生这种情况的原因是python.org可能有某种机制来检测它何时从脚本获得多个请求,并且可能只是在4-5个请求之后完全停止为页面服务。除了在不同的站点上尝试脚本之外,没有什么可以避免的。

票数 0
EN

Stack Overflow用户

发布于 2014-07-26 14:43:28

如前所述,您可以尝试使用代理来避免在多个请求上检测到。您可能需要查看这个答案,以了解如何使用代理发送urllib请求:如何使用urllib通过代理- Python打开网站

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14490978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档