urllib库如何在python中使用

  介绍

今天就跟大家聊聊有关urllib库如何在python中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1,请求模块:urllib。请求

python2

进口urllib2
反应=urllib2.urlopen (& # 39; http://httpbin.org/robots.txt& # 39;)

python3

进口urllib。请求
res=urllib.request.urlopen (& # 39; http://httpbin.org/robots.txt& # 39;)
urllib.request.urlopen (url, data=https://www.yisu.com/zixun/None,超时,*,cafile=None, capath=None, cadefault=False,上下文=None)
urlopen()方法中的url参数可以是字符串,也可以是一个请求对象

# url可以是字符串   import  urllib.request      时间=resp  urllib.request.urlopen (& # 39; http://www.baidu.com& # 39;)   打印(resp.read () .decode (& # 39; utf - 8 # 39;)),, #,阅读()获取响应体的内容,内容是字节字节流,需要转换成字符串 # # url可以也是请求对象   import  urllib.request      时间=request  urllib.request.Request (& # 39; http://httpbin.org& # 39;)   时间=response  urllib.request.urlopen(请求)   打印(response.read () .decode (& # 39; utf - 8 # 39;))

数据参数:帖子请求

#, use utf8编码   import  urllib.request urllib.parse      data =,字节(urllib.parse.urlencode({& # 39;词# 39;:,& # 39;你好# 39;}),,编码=& # 39;use utf8 # 39;)   时间=resp  urllib.request.urlopen (& # 39; http://httpbin.org/post& # 39;,, data=https://www.yisu.com/zixun/data)   print (resp.read ())

urlopen()中的参数超时:设置请求超时时间:

#, use utf8编码   #设置请求超时时间   import  urllib.request      时间=resp  urllib.request.urlopen (& # 39; http://httpbin.org/get& # 39;,,超时=0.1)   打印(resp.read () .decode (& # 39; utf - 8 # 39;))

响应类型:

#, use utf8编码   #响应类型   import  urllib.request      时间=resp  urllib.request.urlopen (& # 39; http://httpbin.org/get& # 39;)   print(类型(职责)

 urllib库如何在python中使用“> </p> <h4>响应的状态码,响应头:</h4> <pre类= #, use utf8编码   #响应的状态码,响应头   import  urllib.request      时间=resp  urllib.request.urlopen (& # 39; http://www.baidu.com& # 39;)   打印(resp.status)   print (resp.getheaders()),, #,数组(元组列表)   print (resp.getheader(& # 39;服务器# 39;)),,#,“Server"大小写不区分

200
[(& # 39; Bdpagetype& # 39; & # 39; 1 & # 39;), (& # 39; Bdqid& # 39; & # 39; 0 xa6d873bb003836ce& # 39;), (& # 39; cache - control # 39; & # 39;私人# 39;),(& # 39;内容类型# 39;& # 39;text/html # 39;), (& # 39; Cxy_all& # 39; & # 39;百度+ b8704ff7c06fb8466a83df26d7f0ad23& # 39;),(& # 39;日期# 39;& # 39;太阳,2019年4月21日15:18:24格林尼治时间# 39;),(& # 39;到期# 39;& # 39;太阳,2019年4月21日15:18:03格林尼治时间# 39;),(& # 39;p3p # 39; & # 39; CP=?OTI DSP和IVA我们印第安纳COM“& # 39;),(& # 39;服务器# 39;& # 39;受虐妇女综合症/1.1 & # 39;),(& # 39;set - cookie # 39; & # 39; BAIDUID=8 c61c3a67c1281b5952199e456eec61e: FG=1;到期=星期四,31-Dec-37 23:55:55格林尼治时间;信息=2147483647;路径=/;域=.baidu.com& # 39;) (& # 39; set - cookie # 39;, & # 39; BIDUPSID=8 c61c3a67c1281b5952199e456eec61e;到期=星期四,31-Dec-37 23:55:55格林尼治时间;信息=2147483647;路径=/;域=.baidu.com& # 39;) (& # 39; set - cookie # 39;, & # 39; PSTM=1555859904;到期=星期四,31-Dec-37 23:55:55格林尼治时间;信息=2147483647;路径=/;域=.baidu.com& # 39;) (& # 39; set - cookie # 39;, & # 39; delPer=0;路径=/;域=.baidu.com& # 39;) (& # 39; set - cookie # 39;, & # 39; BDSVRTM=0;路径=/& # 39;),(& # 39;set - cookie # 39; & # 39; BD_HOME=0;路径=/& # 39;),(& # 39;set - cookie # 39; & # 39; H_PS_PSSID=1452 _28777_21078_28775_28722_28557_28838_28584_28604;路径=/;域=.baidu.com& # 39;)(& # 39;不同# 39;,& # 39;Accept-Encoding& # 39;), (& # 39; X-Ua-Compatible& # 39; & # 39; IE=,=1 & # 39;铬),(& # 39;连接# 39;& # 39;关闭# 39;),(& # 39;Transfer-Encoding& # 39; & # 39;分块# 39;)]
受虐妇女综合症/1.1

使用代理:urllib.request.ProxyHandler ():

#, use utf8编码   时间=proxy_handler  urllib.request.ProxyHandler ({& # 39; http # 39;:, & # 39; http://www.example.com: 3128/& # 39;})   时间=proxy_auth_handler  urllib.request.ProxyBasicAuthHandler ()   proxy_auth_handler.add_password(& # 39;领域# 39;,,& # 39;主机# 39;,,& # 39;用户名# 39;,,& # 39;密码# 39;)      时间=opener  urllib.request.build_opener (proxy_handler, proxy_auth_handler)   #,却,能够一次,,rather  than  install 从而OpenerDirector,, use 我方表示歉意it 直接:   时间=resp  opener.open (& # 39; http://www.example.com/login.html& # 39;)   print (resp.read ())

urllib库如何在python中使用