自动调用随机User-Agent和IP
本模块中构建了用户代理池,IP是接口调用的,这样IP不容易失效,如果自己有稳定的IP也可以构建IP池
def ua_ip():
import random
import urllib.request
UApools = [
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
]
thisip = urllib.request.urlopen("!!!!").read().decode("utf-8", "ignore")
thisua = random.choice(UApools)
headers = ("User-Agent", thisua)
proxy = urllib.request.ProxyHandler({"http":thisip})
opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
opener.addheaders=[headers]
urllib.request.install_opener(opener)
print("\n当前使用的IP地址为:" + thisip)
print("当前使用的用户代理为:" + thisua + "\n")
图片圈起部分把!!!!换成自己代理IP的API链接即可,API链接的字段中设置count=1,这样每次只拿一个IP来用更不浪费
把模块保存成.py文件,把它放进python文件夹的Lib文件夹里,或者就放在自己练习的同一目录下,通过from 模块 import *来调用,效果如下 扫码添加好友,回复:ua_ip模块,即可获取此模块,有问题可以私聊我
|