标签:读取 类型 爬虫 响应 util 练习 获取 tps 分享图片
urllib是一个包,这个包收集了几个用于处理URLs的模块
urllib.request 用于打开和读取URLs urllib.error 用于触发请求的异常 urllib.parse 用于分析URLs urllib.robotparser 用于分析robots.txt格式的文件
import urllib.request response = urllib.request.urlopen("http://www.baidu.com") print(response.read().decode("utf-8"))
import urllib.request import urllib.parse data = bytes(urllib.parse.urlencode({"word":"hello"}),encoding="utf8") response = urllib.request.urlopen("http://httpbin.org/post",data=data) print(response.read())
import urllib.request response = urllib.request.urlopen("http://httpbin.org/get",timeout=1) print(response.read())
import socket import urllib.request import urllib.error try: response = urllib.request.urlopen("http://httpbin.org/get",timeout=0.1) except urllib.error.URLError as e: if isinstance(e.reason,socket.timeout): print("TIME OUT")
import urllib.request response = urllib.request.urlopen("https://www.python.org") print(type(response))
import urllib.request response = urllib.request.urlopen("https://www.python.org") print(response.status) print(response.getheaders()) print(response.getheader("Server"))
import urllib.request request = urllib.request.Request("https://www.python.org") response = urllib.request.urlopen(request) print(response.read().decode("utf-8"))
标签:读取 类型 爬虫 响应 util 练习 获取 tps 分享图片
原文地址:https://www.cnblogs.com/cangshuchirou/p/9726499.html