我试图通过Python中的代理服务器访问Web。我正在使用请求库,我在验证我的代理时遇到问题,因为我使用的代理需要密码。
proxyDict = {
'http' : 'username:mypassword@77.75.105.165',
'https' : 'username:mypassword@77.75.105.165'
}
r = requests.get("http://www.google.com", proxies=proxyDict)
我收到以下错误:
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
r = requests.get("http://www.google.com", proxies=proxyDict)
File "C:\Python27\lib\site-packages\requests\api.py", line 78, in get
:param url: URL for the new :class:`Request` object.
File "C:\Python27\lib\site-packages\requests\api.py", line 65, in request
"""Sends a POST request. Returns :class:`Response` object.
File "C:\Python27\lib\site-packages\requests\sessions.py", line 187, in request
def head(self, url, **kwargs):
File "C:\Python27\lib\site-packages\requests\models.py", line 407, in send
"""
File "C:\Python27\lib\site-packages\requests\packages\urllib3\poolmanager.py", line 127, in proxy_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 521, in connection_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 497, in get_host
ValueError: invalid literal for int() with base 10: 'h6f2v6jh5dsxa@77.75.105.165'
我该如何解决这个问题?
提前感谢您的帮助。
答案 0 :(得分:15)
您应该从proxyDict
中删除嵌入的用户名和密码,然后使用auth
参数。
import requests
from requests.auth import HTTPProxyAuth
proxyDict = {
'http' : '77.75.105.165',
'https' : '77.75.105.165'
}
auth = HTTPProxyAuth('username', 'mypassword')
r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth)
答案 1 :(得分:2)
我在Windows上遇到过类似的问题,发现让requests
工作的唯一方法是在我启动Python之前将代理设置为环境变量。对你来说,这将是这样的:
set HTTP_PROXY=http://77.75.105.165
set HTTPS_PROXY=https://77.75.105.165
您可能还想检查是否需要特定端口,如果是,请在网址后面设置。例如,如果端口是8443,则执行:
set HTTP_PROXY=http://77.75.105.165:8443
set HTTPS_PROXY=https://77.75.105.165:8443
答案 2 :(得分:0)
您可以使用urllib
库。
from urllib import request
request.urlopen("your URL", proxies=urllib.getproxies())