fas*_*ssn 5 proxy python-3.x python-requests
昨天我一整天都在为此挠头,令我惊讶的是,似乎找不到一种简单的方法来检查这一点。
我正在使用 Python 的 Requests 库来传递我的代理,例如:
def make_request(url):
with requests.Session() as s:
s.mount("http://", HTTPAdapter(max_retries=3))
s.mount("https://", HTTPAdapter(max_retries=3))
page = None
d.rotate(-1) #d contains a dict of my proxies. this allows to rotate through the proxies everytime make_request is called.
s.proxies = d[0]
page = s.get(url, timeout=3)
print('proxy used: ' + str(d[0]))
return page.content
Run Code Online (Sandbox Code Playgroud)
问题是,当代理不希望工作时,我似乎无法使请求失败。如果代理不工作,我的互联网 ip 似乎总是有后备。例如:我尝试传递像 101.101.101.101:8800 这样的随机代理 ip 或删除我的代理所需的 ip 身份验证,请求仍然通过,即使它不应该通过。
我认为在传递请求时添加超时参数可以解决问题,但显然没有。
所以
从我到目前为止所看到的来看,你应该使用以下形式
s.get(url, proxies = d)
Run Code Online (Sandbox Code Playgroud)
这应该使用字典中的代理d来建立连接。该表格允许我检查工作代理和非工作代理status_code
print(s.status_code)
一旦我发现它是否只是在字典中的代理上循环以匹配工作代理,或者能够实际选择要使用的代理,我将进行更新。
[更新]
尝试解决代理中的字典问题,如果我愿意的话可以使用不同的代理。然而,proxies必须是一个有效的命令。所以我使用了以下形式的字典:
d = {"https" : 'https://' + str(proxy_ips[n].strip('\n'))}
这似乎有效并允许我使用我想要的 IP。虽然看起来很无聊,但还是希望有人能来帮忙!
使用的代理可以通过以下方式查看:
requests.utils.getproxies()
Run Code Online (Sandbox Code Playgroud)
或者
requests.utils.get_environ_proxies(url)
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助,显然是一个很老的问题,但仍然如此!
| 归档时间: |
|
| 查看次数: |
3263 次 |
| 最近记录: |