以这种遍历的方式爬信息,应该是每遍历一个地址都对应有一次请求吧,那每次请求的时候也应该是随机的请求头才对,但是我设置的随机请求头在遍历range(5, 1000)整个过程从来没有换过user_agent,只有重新运行程序才会出现新的user_agent。所以我对这个随机请求头有什么误解吧!还是range(5, 1000)只发生了一次请求?如何实现随每次请求更换user_agent呢?(单个爬虫,非框架)
user_agent = [
"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/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"]
headers = {'User-Agent': random.choice(user_agent)}
urls = [f'https://api.bilibili.com/x/web-interface/view?aid={av_num}' for av_num in range(5, 1000)]
for url in urls:
response = requests.request("get", url, headers=headers)
print(headers)