首页 > 解决方案 > 如何在抓取时不被阻塞

问题描述

我正在尝试抓取足球网站 Transfermarkt。我正在尝试进行网络抓取,但每次尝试我都会在第 7 次请求时被阻止。

我尝试更改标头和代理,但我总是得到相同的结果。

这些是我做的一些“实验”。这些代理分开工作。

user_agent_list = [here are a lot of user agents]
headers = {'User-Agent':random.choice(user_agent_list)}
url='https://www.transfermarkt.es/jadon-sancho/profil/spieler/14'

r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://121.121.117.227:3128'})
print(r)
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://121.121.117.227:3128'})
print(r)
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://121.121.117.227:3128'})
print(r)

#Changing proxy
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://177.131.22.186:80'})
print(r)
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://177.131.22.186:80'})
print(r)
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://177.131.22.186:80'})
print(r)
#Here I get blocked
r=requests.get(url,headers='User-Agent':random.choice(user_agent_list),proxies={'http': 'http://177.131.22.186:80'})
print(r)
#And continue trying with another examples

我不得不说代理是经过验证的,所以然后单独工作。我从 prints 中得到的是,直到我被阻止,我得到 . 我该如何解决?我应该从 get 更改另一个参数吗?

标签: pythonweb-scrapingpython-requests

解决方案


您的脚本的主要问题是您正在尝试使用代理连接到https服务器。http only您需要设置代理https

proxies={'https': 'https://x.y.z.a:b'}

在您的情况下,您只是设置http代理,因此https请求不会通过它。

请注意,您在示例中给出的代理服务器不支持https.


推荐阅读