码迷,mamicode.com
首页 > Web开发 > 详细

urllib

时间:2015-11-30 13:03:36      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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))

 

urllib

标签:

原文地址:http://www.cnblogs.com/jzm17173/p/5006702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!