关于python 3.X下的urllib,urllib2的使用

先贴一段笔者之前的代码
···
def load_page(url):

# ...
#     发送url请求
#     返回url请求html页面
# ...
user_agent="Mozilla/5.0(compatible; MSIE 9.0; Window NT 6.1; Trident/5.0;"
hearers = { "User_Agent":user_agent}

req = urllib.request.Request(url, headers = hearers)

response = urllib.request.urlopen(req)

html = response.read()
# print (html)
return html

···

在python 3.X下,urllib2.request被替换为urllib.request.Request,urllib2.urlopen被替换被urllib.request.urlopen。也就是现在的urllib拥有之前的urllib和urllib2的功能。

Python3 如何对url解码?实现Python2中urllib.unquote的作用?

Python2中,对url解码 可以这样做:

print urllib.unquote("%E6%B5%8B%E8%AF%95abc")

python3取消unquote属性

可以这样做:

import urllib.parse

print(urllib.parse.unquote("%E6%B5%8B%E8%AF%95abc"))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容