码迷,mamicode.com
首页 > 编程语言 > 详细

Python:Urllib库使用

时间:2018-11-22 00:10:42      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:nbsp   头信息   hits   serve   转换   open   参数转换   nec   max-age   

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库使用

标签:nbsp   头信息   hits   serve   转换   open   参数转换   nec   max-age   

原文地址:https://www.cnblogs.com/mq-b/p/9998168.html

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