日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

python urllib庫的使用詳解

瀏覽:51日期:2022-06-22 15:36:34

相關:urllib是python內置的http請求庫,本文介紹urllib三個模塊:請求模塊urllib.request、異常處理模塊urllib.error、url解析模塊urllib.parse。

1、請求模塊:urllib.request

python2

import urllib2response = urllib2.urlopen(’http://httpbin.org/robots.txt’)

python3

import urllib.requestres = urllib.request.urlopen(’http://httpbin.org/robots.txt’)urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)urlopen()方法中的url參數可以是字符串,也可以是一個Request對象

#url可以是字符串import urllib.requestresp = urllib.request.urlopen(’http://www.baidu.com’)print(resp.read().decode(’utf-8’)) # read()獲取響應體的內容,內容是bytes字節(jié)流,需要轉換成字符串

##url可以也是Request對象import urllib.requestrequest = urllib.request.Request(’http://httpbin.org’)response = urllib.request.urlopen(request)print(response.read().decode(’utf-8’))data參數:post請求

# coding:utf8import urllib.request, urllib.parsedata = bytes(urllib.parse.urlencode({’word’: ’hello’}), encoding=’utf8’)resp = urllib.request.urlopen(’http://httpbin.org/post’, data=data)print(resp.read())urlopen()中的參數timeout:設置請求超時時間:

# coding:utf8#設置請求超時時間import urllib.requestresp = urllib.request.urlopen(’http://httpbin.org/get’, timeout=0.1)print(resp.read().decode(’utf-8’))響應類型:

# coding:utf8#響應類型import urllib.requestresp = urllib.request.urlopen(’http://httpbin.org/get’)print(type(resp))

python urllib庫的使用詳解

響應的狀態(tài)碼、響應頭:

# coding:utf8#響應的狀態(tài)碼、響應頭import urllib.requestresp = urllib.request.urlopen(’http://www.baidu.com’)print(resp.status)print(resp.getheaders()) # 數組(元組列表)print(resp.getheader(’Server’)) # 'Server'大小寫不區(qū)分

200[(’Bdpagetype’, ’1’), (’Bdqid’, ’0xa6d873bb003836ce’), (’Cache-Control’, ’private’), (’Content-Type’, ’text/html’), (’Cxy_all’, ’baidu+b8704ff7c06fb8466a83df26d7f0ad23’), (’Date’, ’Sun, 21 Apr 2019 15:18:24 GMT’), (’Expires’, ’Sun, 21 Apr 2019 15:18:03 GMT’), (’P3p’, ’CP=' OTI DSP COR IVA OUR IND COM '’), (’Server’, ’BWS/1.1’), (’Set-Cookie’, ’BAIDUID=8C61C3A67C1281B5952199E456EEC61E:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com’), (’Set-Cookie’, ’BIDUPSID=8C61C3A67C1281B5952199E456EEC61E; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com’), (’Set-Cookie’, ’PSTM=1555859904; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com’), (’Set-Cookie’, ’delPer=0; path=/; domain=.baidu.com’), (’Set-Cookie’, ’BDSVRTM=0; path=/’), (’Set-Cookie’, ’BD_HOME=0; path=/’), (’Set-Cookie’, ’H_PS_PSSID=1452_28777_21078_28775_28722_28557_28838_28584_28604; path=/; domain=.baidu.com’), (’Vary’, ’Accept-Encoding’), (’X-Ua-Compatible’, ’IE=Edge,chrome=1’), (’Connection’, ’close’), (’Transfer-Encoding’, ’chunked’)]BWS/1.1

使用代理:urllib.request.ProxyHandler():

# coding:utf8proxy_handler = urllib.request.ProxyHandler({’http’: ’http://www.example.com:3128/’})proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()proxy_auth_handler.add_password(’realm’, ’host’, ’username’, ’password’)opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)# This time, rather than install the OpenerDirector, we use it directly:resp = opener.open(’http://www.example.com/login.html’)print(resp.read())2、異常處理模塊:urllib.error異常處理實例1:

# coding:utf8from urllib import error, requesttry: resp = request.urlopen(’http://www.blueflags.cn’)except error.URLError as e: print(e.reason)

python urllib庫的使用詳解

異常處理實例2:

# coding:utf8from urllib import error, requesttry: resp = request.urlopen(’http://www.baidu.com’)except error.HTTPError as e: print(e.reason, e.code, e.headers, sep=’n’)except error.URLError as e: print(e.reason)else: print(’request successfully’)

python urllib庫的使用詳解

異常處理實例3:

# coding:utf8import socket, urllib.request, urllib.errortry: resp = urllib.request.urlopen(’http://www.baidu.com’, timeout=0.01)except urllib.error.URLError as e: print(type(e.reason)) if isinstance(e.reason,socket.timeout):print(’time out’)

python urllib庫的使用詳解

3、url解析模塊:urllib.parseparse.urlencode

# coding:utf8from urllib import request, parseurl = ’http://httpbin.org/post’headers = { ’Host’: ’httpbin.org’, ’User-Agent’: ’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36’}dict = {’name’: ’Germey’}data = bytes(parse.urlencode(dict), encoding=’utf8’)req = request.Request(url=url, data=data, headers=headers, method=’POST’)resp = request.urlopen(req)print(resp.read().decode(’utf-8’))

{'args': {},'data': '','files': {},'form': {'name': 'Thanlon'},'headers': {'Accept-Encoding': 'identity','Content-Length': '12','Content-Type': 'application/x-www-form-urlencoded','Host': 'httpbin.org','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'},'json': null,'origin': '117.136.78.194, 117.136.78.194','url': 'https://httpbin.org/post'}add_header方法添加請求頭:

# coding:utf8from urllib import request, parseurl = ’http://httpbin.org/post’dict = {’name’: ’Thanlon’}data = bytes(parse.urlencode(dict), encoding=’utf8’)req = request.Request(url=url, data=data, method=’POST’)req.add_header(’User-Agent’, ’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36’)resp = request.urlopen(req)print(resp.read().decode(’utf-8’))parse.urlparse:

# coding:utf8from urllib.parse import urlparseresult = urlparse(’http://www.baidu.com/index.html;user?id=1#comment’)print(type(result))print(result)

<class ’urllib.parse.ParseResult’>ParseResult(scheme=’http’, netloc=’www.baidu.com’, path=’/index.html’, params=’user’, query=’id=1’, fragment=’comment’)

from urllib.parse import urlparseresult = urlparse(’www.baidu.com/index.html;user?id=1#comment’, scheme=’https’)print(type(result))print(result)

<class ’urllib.parse.ParseResult’>ParseResult(scheme=’https’, netloc=’’, path=’www.baidu.com/index.html’, params=’user’, query=’id=1’, fragment=’comment’)

# coding:utf8from urllib.parse import urlparseresult = urlparse(’http://www.baidu.com/index.html;user?id=1#comment’, scheme=’https’)print(result)

ParseResult(scheme=’http’, netloc=’www.baidu.com’, path=’/index.html’, params=’user’, query=’id=1’, fragment=’comment’)

# coding:utf8from urllib.parse import urlparseresult = urlparse(’http://www.baidu.com/index.html;user?id=1#comment’,allow_fragments=False)print(result)

ParseResult(scheme=’http’, netloc=’www.baidu.com’, path=’/index.html’, params=’user’, query=’id=1’, fragment=’comment’)

parse.urlunparse:

# coding:utf8from urllib.parse import urlunparsedata = [’http’, ’www.baidu.com’, ’index.html’, ’user’, ’name=Thanlon’, ’comment’]print(urlunparse(data))

python urllib庫的使用詳解

parse.urljoin:

# coding:utf8from urllib.parse import urljoinprint(urljoin(’http://www.bai.com’, ’index.html’))print(urljoin(’http://www.baicu.com’, ’https://www.thanlon.cn/index.html’))#以后面為基準

python urllib庫的使用詳解

urlencode將字典對象轉換成get請求的參數:

# coding:utf8from urllib.parse import urlencodeparams = { ’name’: ’Thanlon’, ’age’: 22}baseUrl = ’http://www.thanlon.cn?’url = baseUrl + urlencode(params)print(url)

python urllib庫的使用詳解

4、Cookiecookie的獲取(保持登錄會話信息):

# coding:utf8#cookie的獲取(保持登錄會話信息)import urllib.request, http.cookiejarcookie = http.cookiejar.CookieJar()handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)res = opener.open(’http://www.baidu.com’)for item in cookie: print(item.name + ’=’ + item.value)

python urllib庫的使用詳解

MozillaCookieJar(filename)形式保存cookie

# coding:utf8#將cookie保存為cookie.txtimport http.cookiejar, urllib.requestfilename = ’cookie.txt’cookie = http.cookiejar.MozillaCookieJar(filename)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)res = opener.open(’http://www.baidu.com’)cookie.save(ignore_discard=True, ignore_expires=True)LWPCookieJar(filename)形式保存cookie:

# coding:utf8import http.cookiejar, urllib.requestfilename = ’cookie.txt’cookie = http.cookiejar.LWPCookieJar(filename)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)res = opener.open(’http://www.baidu.com’)cookie.save(ignore_discard=True, ignore_expires=True)讀取cookie請求,獲取登陸后的信息

# coding:utf8import http.cookiejar, urllib.requestcookie = http.cookiejar.LWPCookieJar()cookie.load(’cookie.txt’, ignore_discard=True, ignore_expires=True)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)resp = opener.open(’http://www.baidu.com’)print(resp.read().decode(’utf-8’))

以上就是python urllib庫的使用詳解的詳細內容,更多關于python urllib庫的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日本成人精品| 女人天堂亚洲aⅴ在线观看| 欧美日本不卡高清| 国产一区二区久久久久| 18国产精品| 国产精品观看| 中文字幕亚洲影视| 日韩高清二区| 黑森林国产精品av| 精品视频一区二区三区在线观看 | 婷婷久久免费视频| 欧美午夜精彩| 亚洲精品888| 国产精品视频一区二区三区综合| 综合亚洲自拍| 精品国产第一福利网站| 丝袜美腿亚洲色图| 鲁大师精品99久久久| 免费不卡中文字幕在线| 日韩av一级片| 五月天激情综合网| 国产精品一区二区三区四区在线观看 | 国产亚洲人成a在线v网站| 久久久久久一区二区| 综合国产视频| 成人自拍av| 欧美亚洲二区| 伊人久久亚洲影院| 视频在线不卡免费观看| 美女尤物久久精品| 手机在线电影一区| 日韩欧美2区| 婷婷激情综合| 黄毛片在线观看| 91嫩草精品| 水蜜桃久久夜色精品一区的特点| 国产伊人久久| 亚洲五月综合| 亚洲一区二区网站| 欧美日韩在线二区| 亚洲啊v在线| 国产精品宾馆| 亚洲a成人v| 在线成人直播| 久久人人99| 成人午夜毛片| 久久不见久久见免费视频7 | 一区三区视频| 三级精品视频| 四虎成人av| 欧美国产免费| 欧美亚洲福利| 四虎国产精品免费久久| 亚洲少妇自拍| 国产字幕视频一区二区| 伊人久久av| 久久精品一区二区国产| 中文字幕日本一区二区| 日韩精品一区二区三区免费观影 | 日韩av在线播放网址| 国产精品xxx在线观看| 青草av.久久免费一区| 亚洲精品婷婷| 久久福利毛片| 久久都是精品| 亚洲视频国产精品| 免费在线成人网| 水蜜桃久久夜色精品一区的特点| 黄色日韩在线| 亚洲精华国产欧美| 今天的高清视频免费播放成人| 日韩成人亚洲| 日韩欧美少妇| 久久影视一区| 欧美精品一卡| 视频在线观看一区二区三区| 午夜在线精品偷拍| 亚洲深夜福利在线观看| 亚洲1区在线观看| 久久国产日韩欧美精品| 国产高清亚洲| 成人精品国产亚洲| 精品三级久久| 久久久成人网| 亚洲欧洲午夜| 亚洲精品大片| 欧美日韩xxxx| 老司机免费视频一区二区三区| 久久av超碰| 电影天堂国产精品| 国产精品99免费看| 日韩影院免费视频| 欧美午夜网站| 国内自拍视频一区二区三区| 日韩亚洲一区在线| 欧美特黄一区| 亚洲精品影院在线观看| 国产乱码精品一区二区三区四区| 久久精品理论片| 亚洲美女久久精品| 国产一区清纯| 天堂成人免费av电影一区| 亚洲精品少妇| 你懂的网址国产 欧美| 麻豆视频在线观看免费网站黄| 久久久久久黄| 久久国产99| 欧美亚洲一区二区三区| 成人一区不卡| 午夜久久影院| 久久国内精品自在自线400部| 麻豆免费精品视频| 久久高清免费| 美女国产精品| 91成人精品观看| 精品三级在线观看视频| 久久久影院免费| 日本亚洲不卡| 久久伊人亚洲| 欧洲在线一区| 日韩一级不卡| 亚洲另类av| 欧美一区二区三区久久精品| 国产精品99久久免费| 欧美羞羞视频| 蜜臀av在线播放一区二区三区| 四虎精品永久免费| 蜜桃精品视频| 久久久一本精品| 久久成人亚洲| 日韩av一区二区在线影视| 国产麻豆一区| 群体交乱之放荡娇妻一区二区| 欧美专区一区二区三区| 国产精品多人| 久久亚洲精品中文字幕蜜潮电影| 亚洲+小说+欧美+激情+另类| 精品国产精品国产偷麻豆| 在线国产一区| 麻豆精品在线观看| 亚洲一区二区三区高清不卡| 国产麻豆精品| 鲁鲁在线中文| 中文字幕亚洲精品乱码| 91精品国产自产观看在线| 欧美精品99| 久久精品亚洲人成影院| 日韩一区二区免费看| 中文字幕日韩欧美精品高清在线| 久久99视频| 性欧美精品高清| 丰满少妇一区| 在线精品视频在线观看高清| 国产精品亚洲人成在99www| 欧美日韩水蜜桃| 国产精品久久久久久久免费软件| 欧美羞羞视频| 国产欧美一区二区精品久久久 | 亚洲天堂资源| 日韩一区精品视频| 国产精品a久久久久| 电影天堂国产精品| 日韩精品一区二区三区免费视频| 牛牛精品成人免费视频| 亚洲一区不卡| 嫩呦国产一区二区三区av| 黑森林国产精品av| 国产精品videossex久久发布| 五月婷婷亚洲| 国产精品一区2区3区| 99在线观看免费视频精品观看| 欧美日韩 国产精品| 麻豆精品蜜桃视频网站| 一区在线免费观看| 久久精品国产999大香线蕉| 99亚洲精品| av资源亚洲| 国产情侣久久| 巨乳诱惑日韩免费av| 蜜桃视频在线网站| 日韩激情综合| 国产精品福利在线观看播放| 日本国产欧美| 红桃视频亚洲| 韩国久久久久久| 久久国产日韩欧美精品| 国产亚洲永久域名| 国产精品99视频| 首页国产欧美日韩丝袜| 亚洲va中文在线播放免费| 久久不见久久见中文字幕免费| 亚洲一二av| 国产午夜久久| 精品视频自拍| 在线国产日韩| 国内精品麻豆美女在线播放视频| 免费日韩成人| 久久精品人人| 国产91在线播放精品| 精品亚洲自拍|