标签:
from urllib import request with request.urlopen(‘https://api.douban.com/v2/book/2129650‘) as f: data = f.read() print(‘Status:‘, f.status, f.reason) for k, v in f.getheaders(): print(‘%s: %s‘ % (k, v)) print(‘Data:‘, data.decode(‘utf-8‘))
模拟浏览器
from urllib import request req = request.Request(‘http://www.douban.com/‘) req.add_header(‘User-Agent‘, ‘Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25‘) with request.urlopen(req) as f: print(‘Status:‘, f.status, f.reason) for k, v in f.getheaders(): print(‘%s: %s‘ % (k, v)) print(‘Data:‘, f.read().decode(‘utf-8‘))
标签:
原文地址:http://www.cnblogs.com/jzm17173/p/5006702.html