python3 urllib加头信息
importurllib.requestheader={'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/...
import urllib.request
header = {'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.4549.400 QQBrowser/9.7.12900.400'}
res = urllib.request.urlopen(url,headers = header)
以前这样加是成功的,现在python3 好像加不成功了。应该怎么弄 展开
header = {'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.4549.400 QQBrowser/9.7.12900.400'}
res = urllib.request.urlopen(url,headers = header)
以前这样加是成功的,现在python3 好像加不成功了。应该怎么弄 展开
1个回答
展开全部
def get(url, headers=None, timeout=2, decode='utf-8'):
rt = HttpReturn()
try:
if headers is None:
hr = urllib.request.urlopen(url, timeout=timeout)
else:
req = urllib.request.Request(url, None, headers)
hr = urllib.request.urlopen(req, timeout=timeout)
rt.obj = hr
rt.text = hr.read().decode(decode)
rt.status = hr.status
finally:
return rt
这是我写的一个函数,你看看应当是这种用法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询