urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
這是urllib.request.urlopen函數(shù),第一個(gè)參數(shù)是網(wǎng)站的url,第二個(gè)參數(shù)是發(fā)送post請(qǐng)求時(shí)需要的數(shù)據(jù),第三個(gè)參數(shù)是超時(shí)的設(shè)置(如果在規(guī)定的時(shí)間沒有返回,則會(huì)報(bào)錯(cuò)),后面的三個(gè)參數(shù)暫時(shí)用不到,不過多敘述。
import urllib.requestresponse = urllib.request.urlopen('http://www.baidu.com')print(response.read().decode('utf-8'))
.read()方法會(huì)返回網(wǎng)頁(yè)的源代碼,這里需要使用utf-8進(jìn)行解碼。
import urllib.requestimport urllib.parsedata = bytes(urllib.parse.urlencode({'word':'hello'}), encoding='utf8')response = urllib.request.urlopen('http://httpbin.org/post',data=data)print(response.read())
這里會(huì)輸出一些json字符串,這個(gè)網(wǎng)址是用來做http測(cè)試的一個(gè)網(wǎng)址,它會(huì)返回一些請(qǐng)求測(cè)試數(shù)據(jù)。
http測(cè)試網(wǎng)址:http://httpbin.org
import urllib.requestimport urllib.parseresponse = urllib.request.urlopen('http://httpbin.org/get',timeout=1)print(response.read())
如果沒有超時(shí),則會(huì)成功返回。如果超時(shí),則程序報(bào)錯(cuò)。
import urllib.requestimport urllib.parseresponse = urllib.request.urlopen('http://httpbin.org/get',timeout=1)print(type(response))
這里使用type可以獲取response的響應(yīng)類型,結(jié)果如下。
import urllib.requestimport urllib.parseresponse = urllib.request.urlopen('http://httpbin.org/get',timeout=1)print(response.status)print(response.getheaders())print(response.getheader('Server'))//這里會(huì)返回Server后面的參數(shù)
效果如下:
from urllib import request,parseurl = 'http://httpbin.org/post'headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36','Host':'httpbin.org'}dict = { 'name':'Germey'}data = bytes(parse.urlencode(dict),encoding='utf8')req = request.Request(url=url,data = data,headers=headers,method='POST')response = request.urlopen(req)print(response.read().decode('utf-8'))
這個(gè)代碼和第一個(gè)發(fā)送post請(qǐng)求的代碼有所不同,但是返回結(jié)果相同,這里可以添加更多參數(shù)進(jìn)行請(qǐng)求,在這里傳入了base64編碼后的from-data數(shù)據(jù),headers,以及制定了method為post請(qǐng)求。
構(gòu)造了request請(qǐng)求,返回結(jié)果為:
from urllib import request,parseimport urllib.requestproxy_handler = urllib.request.ProxyHandler({ 'http':'http://127.0.0.1', 'https':'https://127.0.0.1'})#里面的代理ip是你自己的代理的ipopener = urllib.request.build_opener(proxy_handler)#加入代理,構(gòu)造openerresponse = opener.open('http://www.baidu.com')#請(qǐng)求網(wǎng)頁(yè)print(response.read())
import http.cookiejar,urllib.requestcookie = http.cookiejar.CookieJar()#將cookie聲明為cookiejar的對(duì)象hander = urllib.request.HTTPCookieProcessor(cookie)#借助header處理cookieopener = urllib.request.build_opener(hander)#使用build_opener傳入headerresponse = opener.open('http://www.baidu.com')#利用opener打開百度for item in cookie: print(item.name+"="+item.value)#cookie會(huì)被瀏覽器賦值,然后打印出來
具體分析在代碼中,下面是執(zhí)行結(jié)果:
import http.cookiejar,urllib.requestfilename = "cookie.txt"cookie = http.cookiejar.MozillaCookieJar(filename)hander = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(hander)response = opener.open('http://www.baidu.com')cookie.save(ignore_discard=True,ignore_expires=True)
和剛才相似,只是將cookie保存到了本地,可以在cookie沒有實(shí)效之前繼續(xù)調(diào)用。
這里的cookie可以用不同的形式保存,只需要修改
cookie = http.cookiejar.MozillaCookieJar(filename)即可。
以上為本篇博客內(nèi)容,下一次繼續(xù)學(xué)習(xí)request庫(kù)的使用
聯(lián)系客服