基本的請求庫:
urllib庫的一些使用方法
urlopen()方法
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
url: 需要打開的網(wǎng)址
data:Post提交的數(shù)據(jù)
timeout:設置網(wǎng)站的訪問超時時間
urlopen返回對象提供方法:
read() , readline() ,readlines() , fileno() , close() :對HTTPResponse類型數(shù)據(jù)進行操作
info():返回HTTPMessage對象,表示遠程服務器返回的頭信息
getcode():返回Http狀態(tài)碼急灭。如果是http請求秩伞,200請求成功完成;404網(wǎng)址未找到
geturl():返回請求的url
import urllib request
response = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode('utf-8'))
urllib.parse
以post方式發(fā)送
import urllib request
import urllib parse
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding = 'utf-8')
response = urllib.request.urlopen("http://httpbin.org/post",data = data)
print(response.read[])
response = urllib.request.urlopen("http://httpbin.org/post",timeout = 1)
image.png
image.png
image.png