0

我正在使用以下代码登录 facebook。

import http.cookiejar
import urllib


url = 'https://www.facebook.com/login.php'
values = {'email' : 'john@example.com',
      'pass' : 'mypassword' }

data = urllib.parse.urlencode(values)
cookies =  http.cookiejar.CookieJar()

opener = urllib.request.build_opener(
urllib.request.HTTPRedirectHandler(),
urllib.request.HTTPHandler(debuglevel=0),
urllib.request.HTTPSHandler(debuglevel=0),
urllib.request.HTTPCookieProcessor(cookies))

binary_data = data.encode('ascii')
response = opener.open(url, binary_data)
the_page = response.read()
http_headers = response.info()

但是当我在企业代理防火墙后面时,请求总是超时。你能给我一个解决方案来通过代理进行身份验证,以便请求到达远程 facebook 服务器。

4

1 回答 1

0

添加代理处理程序:

opener = urllib.request.build_opener(
    urllib.request.ProxyHandler({'http': r'http://username:password@url:port'}),
    urllib.request.HTTPRedirectHandler(),
    urllib.request.HTTPHandler(debuglevel=0),
    urllib.request.HTTPSHandler(debuglevel=0),
    urllib.request.HTTPCookieProcessor(cookies)
)
于 2013-07-09T12:55:02.760 回答