首页 > 代码库 > 两种方法修改pyhton爬虫的报头

两种方法修改pyhton爬虫的报头

方法一:

import urlib.request

url = ""

headers=("User-Agent","")

opener = urllib.request.build_opener()

opener.addheaders = [headers]

data=http://www.mamicode.com/open(url).read()

 

方法二:

import urllib.request

url = ""

req=urllib.request.Request(url)

req.add_header(‘User-Agent‘,  ‘‘)

data = http://www.mamicode.com/urllib.request.urlopen(req).read()

两种方法修改pyhton爬虫的报头