使用urllib发起请求- urlopen发起请求read/decode/getcode/info/geturl-post请求抽屉网
生活随笔
收集整理的這篇文章主要介紹了
使用urllib发起请求- urlopen发起请求read/decode/getcode/info/geturl-post请求抽屉网
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
引入本文件需要用到的包
import urllib from urllib import request, parse, response
print(rep.getcode())?獲取響應(yīng)頭信息
print(rep.info())?獲取url地址
print(rep.geturl())
? 參數(shù)直接填寫請求對象
rep = request.urlopen(req) print(rep.read().decode('utf-8'))
data = {"phone": "8615896901897", "password": "qweqweqwe1", "oneMonth": "1"}?b.假如請求數(shù)據(jù)中有中文,需要對中文進(jìn)行編碼
?encode將攜帶的參數(shù)轉(zhuǎn)換為bytes類型
data = parse.urlencode(data).encode('utf-8')? c.請求頭
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"}?d. 構(gòu)建請求對象
req = request.Request(url='http://dig.chouti.com/login',data=data,headers=headers )# 發(fā)起請求
rep = request.urlopen(req) print(rep.read().decode('utf-8'))
import urllib from urllib import request, parse, response
一.使用urllib發(fā)起請求
.read() 函數(shù)讀取響應(yīng)中的響應(yīng)數(shù)據(jù)
decode()? 將bytes類型的數(shù)據(jù)轉(zhuǎn)換為str類型
print(rep.getcode())?獲取響應(yīng)頭信息
print(rep.info())?獲取url地址
print(rep.geturl())
二.攜帶請求頭發(fā)送請求
? 1.獲取百度的網(wǎng)頁源代碼
構(gòu)建請求對象
req = request.Request('http://www.baidu.com', headers={'User-Agent': 'python2.7','Host': 'www.baidu.com' })? .使用urlopen函數(shù),發(fā)起請求? 參數(shù)直接填寫請求對象
rep = request.urlopen(req) print(rep.read().decode('utf-8'))
?2.發(fā)起post請求,攜帶參數(shù)
? ---------------------------------抽屜網(wǎng)-------------------
data = {"phone": "8615896901897", "password": "qweqweqwe1", "oneMonth": "1"}?b.假如請求數(shù)據(jù)中有中文,需要對中文進(jìn)行編碼
?encode將攜帶的參數(shù)轉(zhuǎn)換為bytes類型
data = parse.urlencode(data).encode('utf-8')? c.請求頭
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"}?d. 構(gòu)建請求對象
req = request.Request(url='http://dig.chouti.com/login',data=data,headers=headers )# 發(fā)起請求
rep = request.urlopen(req) print(rep.read().decode('utf-8'))
完整代碼
# -*- coding:utf-8 -*- # urllib urllib2 import urllib from urllib import request, parse, response# 使用urllib發(fā)起請求 rep = request.urlopen('http://www.baidu.com') # .read() 函數(shù)讀取響應(yīng)中的響應(yīng)數(shù)據(jù) # print(rep.read()) result = rep.read() # decode() 將bytes類型的數(shù)據(jù)轉(zhuǎn)換為str類型 html = result.decode('utf-8') # print(html)# 獲取響應(yīng)狀態(tài)碼 print(rep.getcode()) # 獲取響應(yīng)頭信息 print(rep.info()) # 獲取url地址 print(rep.geturl())# 攜帶請求頭發(fā)送請求 # 1.構(gòu)建請求對象 req = request.Request('http://www.baidu.com', headers={'User-Agent': 'python2.7','Host': 'www.baidu.com' }) # 2.使用urlopen函數(shù),發(fā)起請求 # 參數(shù)直接填寫請求對象 rep = request.urlopen(req) print(rep.read().decode('utf-8'))# ---------------------------------抽屜網(wǎng)---------- # 3.發(fā)起post請求,攜帶參數(shù) # 攜帶的數(shù)據(jù) data = {"phone": "8615896901897", "password": "qweqweqwe1", "oneMonth": "1"} # 假如請求數(shù)據(jù)中有中文,需要對中文進(jìn)行編碼 # encode將攜帶的參數(shù)轉(zhuǎn)換為bytes類型 data = parse.urlencode(data).encode('utf-8') # 請求頭 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"} # 構(gòu)建請求對象 req = request.Request(url='http://dig.chouti.com/login',data=data,headers=headers ) # 發(fā)起請求 rep = request.urlopen(req) print(rep.read().decode('utf-8'))運行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的使用urllib发起请求- urlopen发起请求read/decode/getcode/info/geturl-post请求抽屉网的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于ar9331 gpio调试遇到的问题
- 下一篇: Unipus读写课程做题工具