python内置的http请求库
urllib.request 请求模块
urlib.error 异常处理模块
urllib.parse url解析模块
urllib.robotparser robots.txt解析模块
相比python2的变化
python2
import urllib2
response = urllib2.urlopen('http://www.biadu.com')
python3
import urllib.request
response = urllib.reqquest.urlopen('http://www.baidu.com')
用法:
urlopen
发送request请求
urllib.request.urlopen(url,data=None,[timeout,]*,cafile-None,cadefault=False,context=None)
get请求
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))
post请求
# http://httpbin.org/posthttp测试用网址,以后可用测试使用
import urllib.parse
import urllib.request
data =- bytest(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8')
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')
注意: except后边不加冒号
import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8')
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read())
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "10", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.7"\n }, \n "json": null, \n "origin": "106.114.219.48, 106.114.219.48", \n "url": "https://httpbin.org/post"\n}\n'
importurllib.request
response=urllib.request.urlopen('http://httpbin.org/get',timeout=1)
print(response.read())
b'{\n "args": {}, \n "headers": {\n "Accept-Encoding": "identity", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.7"\n }, \n "origin": "106.114.219.48, 106.114.219.48", \n "url": "https://httpbin.org/get"\n}\n'
importsocket
importurllib.request
importurllib.error
try:
response=urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
excepturllib.error.URLErrorase:
ifisinstance(e.reason,socket.timeout):
print('TIME OUT')
TIME OUT
In [ ]:
网友评论