faker切换user-agent

import random

import requests

url = "http://tool.yeves.cn"

import faker

fake = faker.Faker()

uaList = []
for i in range(0,10):
    uaList.append(fake.user_agent())

headers = {
    "User-Agent":random.choice(uaList)
}
response = requests.get(url,headers=headers)
print(response.status_code)
print(response.headers)

def request_view(response):
    import webbrowser
    request_url = response.url
    base_url = '<head><base href="%s">' %(request_url)
    base_url = base_url.encode()
    content = response.content.replace(b"<head>",base_url)
    tem_html = open('tmp.html','wb')
    tem_html.write(content)
    tem_html.close()
    webbrowser.open_new_tab('tmp.html')

request_view(response)

  

原文地址:https://www.cnblogs.com/php-linux/p/12485008.html