import urllib.request
import random
from my_fake_useragent import UserAgent
ua = UserAgent()
#用户代理池
print(type(list((ua.random()))))
# uapools = list(ua.random())
# uapools = [
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
# "Avant Browser/1.2.789rel1 (http://www.avantbrowser.com)",
# "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5",
# ]
def UA():
ua = UserAgent()
opener = urllib.request.build_opener()
# thisua = random.choice(uapools)
thisua = ua.random()
ua = ("User-Agent",thisua)
opener.addheaders = [ua]
urllib.request.install_opener(opener)
print("当前使用UA"+ str(thisua))
for i in range(0,10):
# #如果每2次换一次用户代理
# if i %2 == 0:
UA()
url = "https://www.qiushibaike.com/"
data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
print(len(data))