requests模块(二)基础的代理IP 和 UA伪装
# -*-coding:utf-8 -*-import requests
import random,json
#随机UA浏览器伪装
USER_AGENT_LIST = [
'Opera/9.20 (Macintosh; Intel Mac OS X; U; en)',
'Opera/9.0 (Macintosh; PPC Mac OS X; U; en)',
'iTunes/9.0.3 (Macintosh; U; Intel Mac OS X 10_6_2; en-ca)',
'Mozilla/4.76 (X11; U; SunOS 5.8 sun4u)',
'iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2)',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20120813 Firefox/16.0',
'Mozilla/4.77 (X11; I; IRIX;64 6.5 IP30)',
'Mozilla/4.8 (X11; U; SunOS; 5.7 sun4u)',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3870.400 QQBrowser/10.8.4405.400',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
]
ua = random.choice(USER_AGENT_LIST)
headers = {'User-Agent': ua }
print('UA伪装:'+ str(headers))
# 代理IP
get_url = "熊猫代理API接口" #这里使用的是付费的API接口 熊猫代理
count = 0#第几个IP
ip_list = [] #创建储存IP的列表
temp_data = requests.get(url=get_url).text#对网页发起请求
ip_list.clear()
for eve_ip in json.loads(temp_data)["obj"]: #遍历网页从中取出 IP 和 port
ip_list.append({
"ip": eve_ip["ip"],
"port": eve_ip["port"]})
#IP取出完毕(注意proxy的格式)
proxy = {'http':'http://' + str(ip_list["ip"]) + ":" + str(ip_list["port"])} #设置IP格式
print(proxy)
#开始发起请求
url = 'https://www.baidu.com/'
response = requests.get(url=url,headers=headers,proxies=proxy)
print(response.status_code)
本人用的是付费IP 免费的成功率不高 感谢分享! {:5_95:} 学习了 感谢 懂了一点 {:10_254:} {:10_254:} {:9_241:} Mark~ {:5_102:} Mark~
页:
[1]