python - 如何在抓取时不被阻塞
问题描述
我正在尝试抓取足球网站 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 更改另一个参数吗?
解决方案
您的脚本的主要问题是您正在尝试使用代理连接到https
服务器。http only
您需要设置代理https
:
proxies={'https': 'https://x.y.z.a:b'}
在您的情况下,您只是设置http
代理,因此https
请求不会通过它。
请注意,您在示例中给出的代理服务器不支持
https
.
推荐阅读
- nosql - Oracle NoSQL 数据库云服务的服务控制台是否支持 ORDER BY?
- windows - 如何读取 dbf 文件?
- xaml - 在 UWP XAML 中减小 DatePicker 的宽度
- java - 如何创建下拉菜单并使内容下降?
- c - pthard 不能在带有矩阵的 Ubuntu 上工作
- php - p3-profiler 错误,包括卸载插件后
- react-native - 创建 jsconfig.json 时出现错误
- jailbreak - 如何绕过 iXGuard 的越狱检测?
- amazon-web-services - 如何在 AWS Amplify 上自动将 HTTP 重定向到 HTTPS
- jquery - 从每个表中获取所有输入值