我正在尝试使用python请求刮取一个网站。我们只能使用代理刮网站,所以我实现了代码。然而,即使在我使用代理时,它也会禁止我的所有请求,因此我使用了一个网站https://api.ipify.org/?format=json来检查代理是否正常工作。我发现它显示了我的原始IP,甚至在使用代理的时候。代码如下
from concurrent.futures import ThreadPoolExecutor
import string, random
import requests
import sys
http = []
#loading http into the list
with open(sys.argv[1],"r",encoding = "utf-8") as data:
for i in data:
http.append(i[:-1])
data.close()
url = "https://api.ipify.org/?format=json"
def fetch(session, url):
for i in range(5):
proxy = {'http': 'http://'+random.choice(http)}
try:
with session.get(url,proxies = proxy, allow_redirects=False) as response:
print("Proxy : ",proxy," | Response : ",response.text)
break
except:
pass
# @timer(1, 5)
if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=1) as executor:
with requests.Session() as session:
executor.map(fetch, [session] * 100, [url] * 100)
executor.shutdown(wait=True)
我尝试了很多,但不明白我的ip地址是如何显示的,而不是代理ipv4。您将在这里找到代码的输出https://imgur.com/a/z02uSvi
问题是您为
http
设置了代理,并将请求发送到使用https
的网站。解决方案很简单:相关问题 更多 >
编程相关推荐