如何在请求库中使用多个代理,python?
How to use multiple proxies with requests library, python?
我有一个代理列表,我希望 requests
lib
使用它。因为其中一些不起作用我想在每次没有响应时更改代理。
我有这个代码(我只是试过它是否可以这样工作它不是最终的)
import requests
import itertools
HTTP = [List of HTTP proxies...]
HTTPS = [list of https proxies...]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
yield http_proxy
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
yield https_proxy
proxy_result = try_proxies(HTTP, HTTPS)
print(proxy_result)
# proxies = {
# 'http': http_proxy,
# 'https': https_proxy
# }
#
# try:
# res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=5)
# print(res)
# except requests.exceptions.ConnectTimeout:
# print("well at last your tried")
def main():
try_proxies(HTTP, HTTPS)
main()
但它并没有像我希望的那样工作,我知道这是因为它不正确。最好的方法是什么?
编辑:我低调改进了代码,但仍然没有得到我预期的结果,因为它没有测试每个代理,而是测试 https_proxy
的最后一个值
import requests
HTTP = [List of HTTP proxies...]
HTTPS = [List of HTTPS proxies...]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
proxies = {
'http': http_proxy,
'https': https_proxy,
}
try:
res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=20)
print("here")
print(res.status_code)
except requests.exceptions.ConnectTimeout:
print("well at last your tried")
def main():
try_proxies(HTTP, HTTPS)
main()
我是这样解决问题的:
import requests
HTTP = [List of http proxies...]
HTTPS = [List of https proxies]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
yield http_proxy
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
yield https_proxy
test = [p for p in try_proxies(HTTP, HTTPS)]
for proxy in test:
print(f"{proxy}")
proxies = {
'http': proxy
}
try:
res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
print("here")
print(res.status_code)
except requests.exceptions.ConnectTimeout as CT:
print(f"well at last your tried: {CT}")
希望这对其他人有所帮助!
编辑:我删除了 'https': proxy
因为当我删除代理时出现错误!
我有一个代理列表,我希望 requests
lib
使用它。因为其中一些不起作用我想在每次没有响应时更改代理。
我有这个代码(我只是试过它是否可以这样工作它不是最终的)
import requests
import itertools
HTTP = [List of HTTP proxies...]
HTTPS = [list of https proxies...]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
yield http_proxy
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
yield https_proxy
proxy_result = try_proxies(HTTP, HTTPS)
print(proxy_result)
# proxies = {
# 'http': http_proxy,
# 'https': https_proxy
# }
#
# try:
# res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=5)
# print(res)
# except requests.exceptions.ConnectTimeout:
# print("well at last your tried")
def main():
try_proxies(HTTP, HTTPS)
main()
但它并没有像我希望的那样工作,我知道这是因为它不正确。最好的方法是什么?
编辑:我低调改进了代码,但仍然没有得到我预期的结果,因为它没有测试每个代理,而是测试 https_proxy
import requests
HTTP = [List of HTTP proxies...]
HTTPS = [List of HTTPS proxies...]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
proxies = {
'http': http_proxy,
'https': https_proxy,
}
try:
res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=20)
print("here")
print(res.status_code)
except requests.exceptions.ConnectTimeout:
print("well at last your tried")
def main():
try_proxies(HTTP, HTTPS)
main()
我是这样解决问题的:
import requests
HTTP = [List of http proxies...]
HTTPS = [List of https proxies]
def try_proxies(http_proxies, https_proxies):
for proxy_http_element in http_proxies:
http_proxy = proxy_http_element
yield http_proxy
for proxy_https_element in https_proxies:
https_proxy = proxy_https_element
yield https_proxy
test = [p for p in try_proxies(HTTP, HTTPS)]
for proxy in test:
print(f"{proxy}")
proxies = {
'http': proxy
}
try:
res = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
print("here")
print(res.status_code)
except requests.exceptions.ConnectTimeout as CT:
print(f"well at last your tried: {CT}")
希望这对其他人有所帮助!
编辑:我删除了 'https': proxy
因为当我删除代理时出现错误!