1.參數(shù)類型
字典,字典中鍵值對(duì)作為查詢參數(shù)
2.使用方法
1、res = requests.get(url,params=params,headers=headers)2、特點(diǎn): * url為基準(zhǔn)的url地址,不包含查詢參數(shù) * 該方法會(huì)自動(dòng)對(duì)params字典編碼,然后和url拼接
3.示例
import requestsbaseurl = 'http://tieba.baidu.com/f?'params = { 'kw' : '趙麗穎吧', 'pn' : '50'}headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)'}# 自動(dòng)對(duì)params進(jìn)行編碼,然后自動(dòng)和url進(jìn)行拼接,去發(fā)請(qǐng)求res = requests.get(baseurl,params=params,headers=headers)res.encoding = 'utf-8'print(res.text)
1.作用類型
1、針對(duì)于需要web客戶端用戶名密碼認(rèn)證的網(wǎng)站2、auth = ('username','password')
2.通過用戶名賬號(hào)密碼獲取筆記名稱案例
import requestsfrom lxml import etreeimport osclass NoteSpider(object): def __init__(self): self.url = 'http://code.com.cn/Code/aid1904/redis/' self.headers = {'User-Agent':'Mozilla/5.0'} self.auth = ('code','code_2013') # 獲取 def get_html(self): html = requests.get(url=self.url,auth=self.auth,headers=self.headers).text return html # 解析提取數(shù)據(jù) + 把筆記壓縮包下載完成 def parse_page(self): html = self.get_html() xpath_bds = '//a/@href' parse_html = etree.HTML(html) # r_list : ['../','day01','day02','redis_day01.zip'] r_list = parse_html.xpath(xpath_bds) for r in r_list: if r.endswith('zip') or r.endswith('rar'): print(r)if __name__ == '__main__': spider = NoteSpider() spider.parse_page()
思考:爬取具體的筆記文件?
import requestsfrom lxml import etreeimport osclass NoteSpider(object): def __init__(self): self.url = 'http://code.com.cn/Code/redis/' self.headers = {'User-Agent':'Mozilla/5.0'} self.auth = ('code','code_2013') # 獲取 def get_html(self): html = requests.get(url=self.url,auth=self.auth,headers=self.headers).text return html # 解析提取數(shù)據(jù) + 把筆記壓縮包下載完成 def parse_page(self): html = self.get_html() xpath_bds = '//a/@href' parse_html = etree.HTML(html) # r_list : ['../','day01','day02','redis_day01.zip'] r_list = parse_html.xpath(xpath_bds) for r in r_list: if r.endswith('zip') or r.endswith('rar'): file_url = self.url + r self.save_files(file_url,r) def save_files(self,file_url,r): html_content = requests.get(file_url,headers=self.headers,auth=self.auth).content # 判斷保存路徑是否存在 directory = '/home/redis/' filename = directory + r
#適用頻率很高
#if not os.path.exists('路徑'):
# os.makedirs('路徑') 可遞歸創(chuàng)建
# os.mkdir('路徑')不能遞歸創(chuàng)建 if not os.path.exists(directory): os.makedirs(directory)
with open(filename,'wb') as f: f.write(html_content) print(r,'下載成功')if __name__ == '__main__': spider = NoteSpider() spider.parse_page()
1.適用網(wǎng)站及場(chǎng)景
1、適用網(wǎng)站: https類型網(wǎng)站但是沒有經(jīng)過 證書認(rèn)證機(jī)構(gòu) 認(rèn)證的網(wǎng)站2、適用場(chǎng)景: 拋出 SSLError 異常則考慮使用此參數(shù)
2.參數(shù)類型
1、verify=True(默認(rèn)) : 檢查證書認(rèn)證2、verify=False(常用): 忽略證書認(rèn)證# 示例response = requests.get( url=url, params=params, headers=headers, verify=False)
1.定義
1、定義: 代替你原來的IP地址去對(duì)接網(wǎng)絡(luò)的IP地址。2、作用: 隱藏自身真實(shí)IP,避免被封。
2.普通代理
獲取代理IP網(wǎng)站
西刺代理、快代理、全網(wǎng)代理、代理精靈、... ...
參數(shù)類型
1、語法結(jié)構(gòu) proxies = { '協(xié)議':'協(xié)議://IP:端口號(hào)' }2、示例 proxies = { 'http':'http://IP:端口號(hào)', 'https':'https://IP:端口號(hào)' }
示例代碼
(1)使用免費(fèi)普通代理IP訪問測(cè)試網(wǎng)站: http://httpbin.org/get
import requestsurl = 'http://httpbin.org/get'headers = { 'User-Agent':'Mozilla/5.0'}# 定義代理,在代理IP網(wǎng)站中查找免費(fèi)代理IPproxies = { 'http':'http://112.85.164.220:9999', 'https':'https://112.85.164.220:9999'}html = requests.get(url,proxies=proxies,headers=headers,timeout=5).textprint(html)
思考: 建立一個(gè)自己的代理IP池,隨時(shí)更新用來抓取網(wǎng)站數(shù)據(jù)
1.從代理IP網(wǎng)站上,抓取免費(fèi)的代理IP2.測(cè)試抓取的IP,可用的保存在文件中
(2)寫一個(gè)獲取收費(fèi)開放代理的接口
(3)使用隨機(jī)收費(fèi)開放代理IP寫爬蟲
3.私密代理
語法格式
1、語法結(jié)構(gòu)proxies = { '協(xié)議':'協(xié)議://用戶名:密碼@IP:端口號(hào)'}2、示例proxies = { 'http':'http://用戶名:密碼@IP:端口號(hào)', 'https':'https://用戶名:密碼@IP:端口號(hào)'}
示例代碼
import requestsurl = 'http://httpbin.org/get'proxies = { 'http': 'http://309435365:szayclhp@106.75.71.140:16816', 'https':'https://309435365:szayclhp@106.75.71.140:16816',}headers = { 'User-Agent' : 'Mozilla/5.0',}html = requests.get(url,proxies=proxies,headers=headers,timeout=5).textprint(html)
urllib和urllib2關(guān)系
#python2urllib :URL地址編碼urllib2:請(qǐng)求#python3 - 把python2中urllib和urllib2合并urllib.parse:編碼urllib.requests: 請(qǐng)求
打開方式幾常用選項(xiàng)
1、打開瀏覽器,F(xiàn)12打開控制臺(tái),找到Network選項(xiàng)卡2、控制臺(tái)常用選項(xiàng) 1、Network: 抓取網(wǎng)絡(luò)數(shù)據(jù)包 1、ALL: 抓取所有的網(wǎng)絡(luò)數(shù)據(jù)包 2、XHR:抓取異步加載的網(wǎng)絡(luò)數(shù)據(jù)包 3、JS : 抓取所有的JS文件 2、Sources: 格式化輸出并打斷點(diǎn)調(diào)試JavaScript代碼,助于分析爬蟲中一些參數(shù) 3、Console: 交互模式,可對(duì)JavaScript中的代碼進(jìn)行測(cè)試3、抓取具體網(wǎng)絡(luò)數(shù)據(jù)包后 1、單擊左側(cè)網(wǎng)絡(luò)數(shù)據(jù)包地址,進(jìn)入數(shù)據(jù)包詳情,查看右側(cè) 2、右側(cè): 1、Headers: 整個(gè)請(qǐng)求信息 General、Response Headers、Request Headers、Query String、Form Data 2、Preview: 對(duì)響應(yīng)內(nèi)容進(jìn)行預(yù)覽 3、Response:響應(yīng)內(nèi)容
聯(lián)系客服