Python:Urllib库使用
Posted mq-b
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python:Urllib库使用相关的知识,希望对你有一定的参考价值。
import urllib.request response = urllib.request.urlopen("http://www.python.org") print(response.status) #获取响应码 print(response.getheaders()) #获取响应头信息 print(response.getheader("Server")) #获取响应头中Server的值 print(response.getheader(‘Content-Type‘)) """ 运行结果如下: 200 [(‘Server‘, ‘nginx‘), (‘Content-Type‘, ‘text/html; charset=utf-8‘), (‘X-Frame-Options‘, ‘SAMEORIGIN‘), (‘x-xss-protection‘, ‘1; mode=block‘), (‘X-Clacks-Overhead‘, ‘GNU Terry Pratchett‘), (‘Via‘, ‘1.1 varnish‘), (‘Content-Length‘, ‘50114‘), (‘Accept-Ranges‘, ‘bytes‘), (‘Date‘, ‘Wed, 21 Nov 2018 14:28:32 GMT‘), (‘Via‘, ‘1.1 varnish‘), (‘Age‘, ‘1610‘), (‘Connection‘, ‘close‘), (‘X-Served-By‘, ‘cache-iad2141-IAD, cache-lax8624-LAX‘), (‘X-Cache‘, ‘HIT, HIT‘), (‘X-Cache-Hits‘, ‘4, 107‘), (‘X-Timer‘, ‘S1542810512.220409,VS0,VE0‘), (‘Vary‘, ‘Cookie‘), (‘Strict-Transport-Security‘, ‘max-age=63072000; includeSubDomains‘)] nginx text/html; charset=utf-8 """
import urllib data = bytes(urllib.parse.urlencode({"word":"hello"}), encoding="utf8") print(data) #将参数转换成bytes类型 b‘word=hello‘ response = urllib.request.urlopen("http://httpbin.org/post", data=data) print(response.read().decode("utf-8")) import urllib.request import socket import urllib.error try: #timeout实现超时处理 response = urllib.request.urlopen("http://httpbin.org/get", timeout=0.1) except urllib.error.URLError as e: if isinstance(e.reason, socket.timeout): #isinstance判断异常对象是不是时间类型 print("TIME OUT") import urllib request = urllib.request.Request("http://python.org") response = urllib.request.urlopen(request) print(response.read().decode("utf-8"))
以上是关于Python:Urllib库使用的主要内容,如果未能解决你的问题,请参考以下文章