import requests
import random
url = "https://www.baidu.com" #访问页面
ip_list = [ #ip存放地址
'223.159.28.152:8080',
'113.200.105.45:8080',
'110.229.233.76:8080',
'125.79.15.140:8888'
]
def change_ip_info():
#不同类型的操作系统以及浏览器的标识
user_agent_list=[
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
]
#请求是从哪个链接而来的
referer_list=[
'https://www.sogou.com/',
'https://www.so.com/',
# 'https://www.baidu.com/',
]
#随机抽取user_agent和referer设置Headers识别
header = {
'User-Agent':random.choice(user_agent_list),
'Referer':random.choice(referer_list)
}
ip=random.choice(ip_list)
proxy_ip = 'http://' + ip
proxy_ips = 'https://' + ip
proxy = {'https': proxy_ips, 'http': proxy_ip}
try:
html=requests.get(url,headers=header, proxies=proxy,timeout=(3,7)) #尝试访问是否成功
except: #异常抛出
print("访问失败!")
else:
print("成功访问!")
if __name__=='__main__':
change_ip_info()
python爬虫,随机更换代理ip,User-Agent, Referer,方法
最新推荐文章于 2024-04-15 16:30:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)