【问题标题】:python: how to use/change proxy with mechanizepython:如何使用/更改代理与机械化
【发布时间】:2014-08-20 01:24:18
【问题描述】:

我正在使用 mechanize 在 python 中编写一个网页抓取程序。我遇到的问题是,我从中抓取的网站限制了您可以在该网站上的时间。当我手动做所有事情时,我会使用 SOCKS 代理作为解决方法。

我尝试做的是转到网络首选项(Macbook Pro Retina 13',mavericks)并更改为代理。但是,该程序没有响应这一变化。它在没有代理的情况下继续运行。

然后我添加了 .set_proxies(),所以现在打开网站的代码如下所示:

b=mechanize.Browser()                               #open browser
b.set_proxies({"http":"96.8.113.76:8080"})          #proxy
DBJ=b.open(URL)                                     #open url

当我运行程序时,我得到了这个错误:

Traceback (most recent call last):
 File "GM1.py", line 74, in <module>
   DBJ=b.open(URL)                  
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_mechanize.py", line 203, in open
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_mechanize.py", line 230, in _mech_open
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_opener.py", line 193, in open
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py", line 344, in _open
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py", line 1142, in http_open
 File "build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py", line 1118, in do_open
urllib2.URLError: <urlopen error [Errno 54] Connection reset by peer>

我假设代理已更改,并且此错误是对该代理的响应。

也许我在滥用 .set_proxies()。

我不确定代理本身是问题还是连接速度真的很慢。

我什至应该使用 SOCKS 代理来处理这种类型的事情,还是有更好的替代方案来解决我正在尝试做的事情?

任何信息都会非常有帮助。提前致谢。

【问题讨论】:

    标签: python proxy web-scraping mechanize


    【解决方案1】:

    SOCKS 代理与 HTTP 代理不同。客户端和代理之间的协议是不同的。行:

    b.set_proxies({"http":"96.8.113.76:8080"})
    

    告诉 mechanize 使用 96.8.113.76:8080 的 HTTP 代理来处理 URL 中具有 http 方案的请求,例如URL http://httpbin.org/get 的请求将通过代理在 96.8.113.76:8080 发送。 Mechanize 期望这是一个 HTTP 代理服务器,并使用相应的协议。您的 SOCKS 代理似乎正在关闭连接,因为它没有收到有效的 SOCKS 代理请求(因为它实际上是一个 HTTP 代理请求)。

    我认为 mechanize 没有内置对 SOCKS 的支持,因此您可能不得不诉诸一些肮脏的技巧,例如 answer 中的那些。为此,您需要安装PySocks package。这可能对你有用:

    import socks
    import socket
    from mechanize import Browser
    
    SOCKS_PROXY_HOST = '96.8.113.76'
    SOCKS_PROXY_PORT = 8080
    
    def create_connection(address, timeout=None, source_address=None):
        sock = socks.socksocket()
        sock.connect(address)
        return sock
    
    # add username and password arguments if proxy authentication required.
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, SOCKS_PROXY_HOST, SOCKS_PROXY_PORT)
    
    # patch the socket module
    socket.socket = socks.socksocket
    socket.create_connection = create_connection
    
    br = Browser()
    response = br.open('http://httpbin.org/get')
    
    >>> print response.read()
    {
      "args": {}, 
      "headers": {
        "Accept-Encoding": "identity", 
        "Connection": "close", 
        "Host": "httpbin.org", 
        "User-Agent": "Python-urllib/2.7", 
        "X-Request-Id": "e728cd40-002c-4f96-a26a-78ce4d651fda"
      }, 
      "origin": "192.161.1.100", 
      "url": "http://httpbin.org/get"
    }
    

    【讨论】:

    • 我得到:mechanize._response.httperror_seek_wrapper:HTTP 错误 403:robots.txt 和添加 br.set_handle_robots(False) 后不允许请求:操作超时
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多