python3 带header抓取url

import urllib.request

headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
    (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}

try:
    req = urllib.request.Request(url, None, headers)     #Request用来合并请求
    res = urllib.request.urlopen(req, data = None, timeout = 5).read()
    res = res.decode('utf-8')
except Exception as e:
    print("error===" + url + " %s" % e)

 

一定需要有decode,否则会输出‘b'字符

你可能感兴趣的:(python3 带header抓取url)