日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

requests.get()參數(shù)

 網(wǎng)海拾貝網(wǎng)絡(luò)豬 2020-03-16

查詢參數(shù)-params

1.參數(shù)類型

  字典,字典中鍵值對作為查詢參數(shù)

2.使用方法

1、res = requests.get(url,params=params,headers=headers)
2、特點: 
   * url為基準(zhǔn)的url地址,不包含查詢參數(shù)
   * 該方法會自動對params字典編碼,然后和url拼接

3.示例

復(fù)制代碼
import requests

baseurl = '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)'}
# 自動對params進行編碼,然后自動和url進行拼接,去發(fā)請求
res = requests.get(baseurl,params=params,headers=headers)
res.encoding = 'utf-8'
print(res.text)
復(fù)制代碼

web客戶端驗證 參數(shù)-auth

1.作用類型

1、針對于需要web客戶端用戶名密碼認(rèn)證的網(wǎng)站
2、auth = ('username','password')

2.通過用戶名賬號密碼獲取筆記名稱案例

復(fù)制代碼
import requests
from lxml import etree
import os

class NoteSpider(object):
    def __init__(self):
        self.url = 'http:///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()
復(fù)制代碼

思考:爬取具體的筆記文件?

復(fù)制代碼
import requests
from lxml import etree
import os

class NoteSpider(object):
    def __init__(self):
        self.url = 'http:///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()
復(fù)制代碼

SSL證書認(rèn)證參數(shù)-verify

1.適用網(wǎng)站及場景

1、適用網(wǎng)站: https類型網(wǎng)站但是沒有經(jīng)過 證書認(rèn)證機構(gòu) 認(rèn)證的網(wǎng)站
2、適用場景: 拋出 SSLError 異常則考慮使用此參數(shù)

2.參數(shù)類型

復(fù)制代碼
1、verify=True(默認(rèn))   : 檢查證書認(rèn)證
2、verify=False(常用): 忽略證書認(rèn)證
# 示例
response = requests.get(
    url=url,
    params=params,
    headers=headers,
    verify=False
)
復(fù)制代碼

代理參數(shù)-proxies 

1.定義

1、定義: 代替你原來的IP地址去對接網(wǎng)絡(luò)的IP地址。
2、作用: 隱藏自身真實IP,避免被封。

2.普通代理

  獲取代理IP網(wǎng)站

西刺代理、快代理、全網(wǎng)代理、代理精靈、... ... 

  參數(shù)類型

復(fù)制代碼
1、語法結(jié)構(gòu)
       proxies = {
           '協(xié)議':'協(xié)議://IP:端口號'
       }
2、示例
    proxies = {
        'http':'http://IP:端口號',
        'https':'https://IP:端口號'
    }
復(fù)制代碼

  示例代碼

    (1)使用免費普通代理IP訪問測試網(wǎng)站: http:///get

復(fù)制代碼
import requests

url = 'http:///get'
headers = {
    'User-Agent':'Mozilla/5.0'
}
# 定義代理,在代理IP網(wǎng)站中查找免費代理IP
proxies = {
    'http':'http://112.85.164.220:9999',
    'https':'https://112.85.164.220:9999'
}
html = requests.get(url,proxies=proxies,headers=headers,timeout=5).text
print(html)
復(fù)制代碼

    考: 建立一個自己的代理IP池,隨時更新用來抓取網(wǎng)站數(shù)據(jù)

1.從代理IP網(wǎng)站上,抓取免費的代理IP
2.測試抓取的IP,可用的保存在文件中

    (2)一個獲取收費開放代理的接口

實現(xiàn)代碼

    (3)使用隨機收費開放代理IP寫爬蟲

實現(xiàn)代碼

3.私密代理

  語法格式

復(fù)制代碼
1、語法結(jié)構(gòu)
proxies = {
    '協(xié)議':'協(xié)議://用戶名:密碼@IP:端口號'
}

2、示例
proxies = {
    'http':'http://用戶名:密碼@IP:端口號',
    'https':'https://用戶名:密碼@IP:端口號'
}
復(fù)制代碼

  示例代碼

復(fù)制代碼
import requests
url = 'http:///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).text
print(html)
復(fù)制代碼

urllib和urllib2關(guān)系

復(fù)制代碼
#python2
urllib :URL地址編碼
urllib2:請求
#python3 - 把python2中urllib和urllib2合并
urllib.parse:編碼
urllib.requests: 請求
復(fù)制代碼

控制臺抓包

打開方式幾常用選項

復(fù)制代碼
1、打開瀏覽器,F(xiàn)12打開控制臺,找到Network選項卡
2、控制臺常用選項
   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ào)試JavaScript代碼,助于分析爬蟲中一些參數(shù)
   3、Console: 交互模式,可對JavaScript中的代碼進行測試
3、抓取具體網(wǎng)絡(luò)數(shù)據(jù)包后
   1、單擊左側(cè)網(wǎng)絡(luò)數(shù)據(jù)包地址,進入數(shù)據(jù)包詳情,查看右側(cè)
   2、右側(cè):
       1、Headers: 整個請求信息
            General、Response Headers、Request Headers、Query String、Form Data
       2、Preview: 對響應(yīng)內(nèi)容進行預(yù)覽
       3、Response:響應(yīng)內(nèi)容
復(fù)制代碼

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多