一、requests设置请求头:
import requests
url="http://www.targetweb.com"
headers={
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,**;q=0.8',
'accept-language': 'zh-CN,zh;q=0.8',
'referer': 'https://www.baidu.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400',}五、Python异步Aiohttp请求头设置:
import aiohttp
url="http://www.targetweb.com"
headers={
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Referer':'http://www.baidu.com/',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400'}
asyncwithaiohttp.ClientSession(headers=headers)assession:
asyncwithsession.get(url)asresp:
print(resp.status)
print(awaitresp.text())更多Python知识请关注Python自学网



