使用代理时python请求出现问题

2024-05-15 00:45:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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


Tags: 代码httpsimportapihttpurl代理data
1条回答
网友
1楼 · 发布于 2024-05-15 00:45:10

问题是您为http设置了代理,并将请求发送到使用https的网站。解决方案很简单:

proxies = dict.fromkeys(('http', 'https', 'ftp'), 'http://' + random.choice(http))
# You can set proxy for session
session.proxies.update(proxies)
response = session.get(url)
# Or you can pass proxy as argument
response = session.get(url, proxies=proxies)

相关问题 更多 >

    热门问题