国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python3學(xué)習(xí)urllib的使用方法示例

2020-02-16 10:53:25
字體:
供稿:網(wǎng)友

urllib是python的一個(gè)獲取url(Uniform Resource Locators,統(tǒng)一資源定址符)了,可以利用它來抓取遠(yuǎn)程的數(shù)據(jù)進(jìn)行保存,本文整理了一些關(guān)于urllib使用中的一些關(guān)于header,代理,超時(shí),認(rèn)證,異常處理處理方法。

1.基本方法

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

    url:  需要打開的網(wǎng)址 data:Post提交的數(shù)據(jù) timeout:設(shè)置網(wǎng)站的訪問超時(shí)時(shí)間

直接用urllib.request模塊的urlopen()獲取頁面,page的數(shù)據(jù)格式為bytes類型,需要decode()解碼,轉(zhuǎn)換成str類型。

from urllib import requestresponse = request.urlopen(r'http://python.org/') # <http.client.HTTPResponse object at 0x00000000048BC908> HTTPResponse類型page = response.read()page = page.decode('utf-8')

urlopen返回對象提供方法:

    read() , readline() ,readlines() , fileno() , close() :對HTTPResponse類型數(shù)據(jù)進(jìn)行操作 info():返回HTTPMessage對象,表示遠(yuǎn)程服務(wù)器返回的頭信息 getcode():返回Http狀態(tài)碼。如果是http請求,200請求成功完成;404網(wǎng)址未找到 geturl():返回請求的url

1、簡單讀取網(wǎng)頁信息

import urllib.request response = urllib.request.urlopen('http://python.org/') html = response.read() 

2、使用request

urllib.request.Request(url, data=None, headers={}, method=None)

使用request()來包裝請求,再通過urlopen()獲取頁面。

import urllib.request req = urllib.request.Request('http://python.org/') response = urllib.request.urlopen(req) the_page = response.read() 

3、發(fā)送數(shù)據(jù),以登錄知乎為例

''''' Created on 2016年5月31日  @author: gionee ''' import gzip import re import urllib.request import urllib.parse import http.cookiejar  def ungzip(data):   try:     print("嘗試解壓縮...")     data = gzip.decompress(data)     print("解壓完畢")   except:     print("未經(jīng)壓縮,無需解壓")      return data      def getXSRF(data):   cer = re.compile('name=/"_xsrf/" value=/"(.*)/"',flags = 0)   strlist = cer.findall(data)   return strlist[0]  def getOpener(head):   # cookies 處理   cj = http.cookiejar.CookieJar()   pro = urllib.request.HTTPCookieProcessor(cj)   opener = urllib.request.build_opener(pro)   header = []   for key,value in head.items():     elem = (key,value)     header.append(elem)   opener.addheaders = header   return opener # header信息可以通過firebug獲得 header = {   'Connection': 'Keep-Alive',   'Accept': 'text/html, application/xhtml+xml, */*',   'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',   'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',   'Accept-Encoding': 'gzip, deflate',   'Host': 'www.zhihu.com',   'DNT': '1' }  url = 'http://www.zhihu.com/' opener = getOpener(header) op = opener.open(url) data = op.read() data = ungzip(data) _xsrf = getXSRF(data.decode())  url += "login/email" email = "登錄賬號" password = "登錄密碼" postDict = {   '_xsrf': _xsrf,   'email': email,   'password': password,   'rememberme': 'y'  } postData = urllib.parse.urlencode(postDict).encode() op = opener.open(url,postData) data = op.read() data = ungzip(data)  print(data.decode())             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 澳门| 巫溪县| 肇东市| 西林县| 陆河县| 宁波市| 平定县| 南宁市| 八宿县| 武邑县| 舟山市| 平远县| 积石山| 宁城县| 凤凰县| 分宜县| 吴堡县| 新河县| 闻喜县| 荆门市| 南汇区| 台前县| 滦南县| 股票| 太仆寺旗| 深水埗区| 合作市| 东乡| 闻喜县| 双城市| 上杭县| 旬阳县| 吴桥县| 海城市| 准格尔旗| 拉孜县| 溧水县| 诏安县| 乐昌市| 正安县| 怀宁县|