【问题标题】:Python web crawler : Connection Timed outPython网络爬虫:连接超时
【发布时间】:2013-01-23 22:58:38
【问题描述】:

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

fetcher.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:

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 个链接运行良好,然后挂起,一分钟后出现以下错误:

[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 很陌生,我无法找出为什么它说连接超时一段时间后? p>

【问题讨论】:

    标签: python web-crawler beautifulsoup


    【解决方案1】:

    Connection Timeout 并非特定于 python,它只是表示您向服务器发出请求,而服务器在您的应用程序愿意等待的时间内没有响应。

    发生这种情况的一个很可能的原因是 python.org 可能有一些机制来检测它何时从脚本获得多个请求,并且可能在 4-5 个请求后完全停止服务页面。除了在不同的网站上尝试您的脚本之外,您实际上无法采取任何措施来避免这种情况。

    【讨论】:

    • 是的。并且每次链接的确切数量后都会卡住。我与其他网站运行相同的脚本,但它没有给我这个问题。 :) 感谢您的回复。
    【解决方案2】:

    您可以尝试使用代理来避免在如上所述的多个请求中被检测到。您可能想查看此答案以了解如何使用代理发送 urllib 请求:How to open website with urllib via Proxy - Python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 2013-10-15
      • 2013-01-09
      • 1970-01-01
      相关资源
      最近更新 更多