Python 网络框架UrlLib 请求网络的三种方式

版权声明:欢迎转载,但请声明出处http://blog.csdn.net/dazhaodai https://blog.csdn.net/dazhaoDai/article/details/80923453

最近在看Python内容,在慕课网学习中,学习三种请求方法,先记录下来,上代码

Python版本:Python 3.6
IDE:pycharm

from urllib import request
import http.cookiejar

url = 'https://www.douban.com/'

print ('第一种方法')

with request.urlopen(url) as response1:
    print(response1.getcode())
    print(len(response1.read()))
print('\n')
print('第二种方法')


re = request.Request(url)
re.add_header('user-agent','Mozilla/5.0')
with request.urlopen(re) as response2:
    print(response2.getcode())
    print(len(response2.read()))


print('\n')
print('第三种方法')

cj = http.cookiejar.CookieJar()

opener = request.build_opener(request.HTTPCookieProcessor(cj))
request.install_opener(opener)
with request.urlopen(url) as response3:
    print(response3.getcode())
    print(len(response3.read()))
    print(cj)

展开阅读全文

没有更多推荐了,返回首页