python:如何通过mechanize使用/更改代理

python:如何通过mechanize使用/更改代理,python,proxy,web-scraping,mechanize,Python,Proxy,Web Scraping,Mechanize,我正在用python编写一个使用mechanize的web抓取程序。我遇到的问题是,我正在浏览的网站限制了你在网站上的时间。当我手工做每件事时,我会使用SOCKS代理作为变通方法 我试图做的是转到网络首选项(Macbook Pro Retina 13',mavericks)并更改为代理。然而,该计划没有对这一变化作出回应。它在没有代理的情况下继续运行 然后我添加了.set_proxies(),因此现在打开网站的代码如下所示: b=mechanize.Browser()

我正在用python编写一个使用mechanize的web抓取程序。我遇到的问题是,我正在浏览的网站限制了你在网站上的时间。当我手工做每件事时,我会使用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>
回溯(最近一次呼叫最后一次):
文件“GM1.py”,第74行,在
DBJ=b.open(URL)
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_mechanize.py”,第203行,打开
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_mechanize.py”,第230行,在“打开”中
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_opener.py”,第193行,打开
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py”,第344行,打开
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_urlib2_fork.py”,第332行,在调用链中
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py”,第1142行,在http_open中
文件“build/bdist.macosx-10.9-intel/egg/mechanize/_urllib2_fork.py”,第1118行,在do_open中
urllib2.URLError:
我假设代理已更改,并且此错误是对该代理的响应

也许我误用了。set_proxies()

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

对于这类事情,我应该使用SOCKS代理还是有更好的替代方案


任何信息都会非常有用。提前感谢。

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

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

我不认为mechanize已经内置了对袜子的支持,所以你可能不得不求助于一些肮脏的技巧,比如下面这些。为此,您需要安装。这可能适合您:

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.\u response.httperror\u seek\u wrapper:HTTP错误403:请求被robots.txt禁止,添加br.set\u handle\u robots后(False):操作超时