国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【python爬蟲(chóng)】設(shè)計(jì)自己的爬蟲(chóng) 1. request封裝

這篇具有很好參考價(jià)值的文章主要介紹了【python爬蟲(chóng)】設(shè)計(jì)自己的爬蟲(chóng) 1. request封裝。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

通過(guò)requests.session().request 封裝request方法
考慮到請(qǐng)求HTTP/2.0
同時(shí)封裝httpx 來(lái)處理HTTP/2.0的請(qǐng)求

封裝requests

# 遇到請(qǐng)求失敗的情況時(shí) 重新請(qǐng)求,請(qǐng)求5次等待2s
@retry(stop_max_attempt_number=5, retry_on_result=lambda re_data: re_data is None, wait_fixed=2000)
    def requests_request(self, method, url, params=None, data=None, json=None, headers=None, files=None, verify=False,
                         cert=None, timeout=None, proxies=None, proxy=None, **kwargs):

        # 對(duì)異常進(jìn)行捕獲
        try:
            """
                封裝request請(qǐng)求,將請(qǐng)求方法、請(qǐng)求地址,請(qǐng)求參數(shù)、請(qǐng)求頭等信息入?yún)ⅰ?                注 :verify: True/False,默認(rèn)為T(mén)rue,認(rèn)證SSL證書(shū)開(kāi)關(guān);cert: 本地SSL證書(shū)。如果不需要ssl認(rèn)證,可將這兩個(gè)入?yún)⑷サ?            
                使用session管理器
                requests.session(): 維持會(huì)話,跨請(qǐng)求的時(shí)候保存參數(shù)   
            """

            # 處理代理
            proxies = None
            if proxy:
                proxies = {
                    'http://': 'http://' + proxy,
                    'https://': 'https://' + proxy,
                }
            #  使用requests.session().request 請(qǐng)求
            re_data = requests.session().request(method, url, params=params, data=data, json=json, headers=headers,
                                                 files=files, cert=cert, timeout=timeout, verify=verify,
                                                 proxies=proxies, **kwargs)
        # 異常處理 報(bào)錯(cuò)顯示具體信息
        except Exception as e:
            re_data = None
            # 打印異常
            print("請(qǐng)求失?。簕0}".format(e))
            logger.error("Error occurred: %s", str(e), exc_info=True)
            # 重新拋出異常,觸發(fā) retry 機(jī)制
            raise e
        # 返回響應(yīng)結(jié)果
        return re_data

封裝httpx

@retry(stop_max_attempt_number=5, retry_on_result=lambda re_data: re_data is None, wait_fixed=2000)
    def httpx_request(self, method, url, is_http2=False, content=None, data=None, files=None, json=None, params=None,
                      headers=None, cookies=None, timeout=None, extensions=None, proxy=None, **kwargs):

        # 對(duì)異常進(jìn)行捕獲
        try:
            """
                使用client  
                method.upper() 請(qǐng)求方法都轉(zhuǎn)為大寫(xiě)
            """
            # 處理代理
            proxies = None
            if proxy:
                proxies = {
                    'http://': 'http://' + proxy,
                    'https://': 'https://' + proxy,
                }

            re_data = httpx.Client(http2=is_http2, proxies=proxies).request(method.upper(), url, content=content,
                                                                            data=data, files=files, json=json,
                                                                            params=params, headers=headers,
                                                                            cookies=cookies, timeout=timeout,
                                                                            extensions=extensions, **kwargs)
        # 異常處理 報(bào)錯(cuò)顯示具體信息
        except Exception as e:
            re_data = None
            # 打印異常
            print("請(qǐng)求失?。簕0}".format(e))
            logger.error("Error occurred: %s", str(e), exc_info=True)
            # 重新拋出異常,觸發(fā) retry 機(jī)制
            raise e
        # 返回響應(yīng)結(jié)果
        return re_data

將兩個(gè)請(qǐng)求封裝在一個(gè)方法里

@retry(stop_max_attempt_number=5, retry_on_result=lambda re_data: re_data is None, wait_fixed=2000)
    def request(self, method, url, is_http2=False, params=None, data=None, json=None, headers=None, files=None,
                verify=False, cert=None, timeout=None, proxies=None, content=None, cookies=None, extensions=None,
                **kwargs):

        try:
            if is_http2:
                re_data = self.httpx_request(method=method.upper(), url=url, is_http2=is_http2, content=content,
                                             data=data, files=files, json=json, params=params, headers=headers,
                                             cookies=cookies, timeout=timeout, extensions=extensions, **kwargs)
            else:
                re_data = self.requests_request(method=method, url=url, params=params, data=data, json=json,
                                                headers=headers, files=files, cert=cert, timeout=timeout, verify=verify,
                                                proxies=proxies, **kwargs)

        # 異常處理 報(bào)錯(cuò)顯示具體信息
        except Exception as e:
            re_data = None
            # 打印異常
            print("請(qǐng)求失?。簕0}".format(e))
            logger.error("Error occurred: %s", str(e), exc_info=True)
            # 重新拋出異常,觸發(fā) retry 機(jī)制
            raise e
        # 返回響應(yīng)結(jié)果
        return re_data

通過(guò)is_http2來(lái)區(qū)分

測(cè)試代碼如下文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-713743.html

if __name__ == '__main__':

    # request_requests 使用requests請(qǐng)求
    request_data = request_main.requests_request("get", 'https://spa16.scrape.center/')
    if request_data:
        print(request_data.text)
        print(request_data.status_code)

    # httpx 請(qǐng)求HTTP/2.0
    # response = re.httpx_request('GET', 'https://spa16.scrape.center/', True)
    # httpx 一般請(qǐng)求
    # headers = {'User-Agent': 'my-app/0.0.1'}
    # response = re.httpx_request('get', 'https://www.httpbin.org/get',params={'name': 'germey'})
    # print(response.text)
    # print(response.status_code)

    print(datetime.datetime.now())

到了這里,關(guān)于【python爬蟲(chóng)】設(shè)計(jì)自己的爬蟲(chóng) 1. request封裝的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【100天精通python】Day42:python網(wǎng)絡(luò)爬蟲(chóng)開(kāi)發(fā)_HTTP請(qǐng)求庫(kù)requests 常用語(yǔ)法與實(shí)戰(zhàn)

    目錄 1 HTTP協(xié)議 2??HTTP與HTTPS 3 HTTP請(qǐng)求過(guò)程 ?3.1 HTTP請(qǐng)求過(guò)程 3.2 GET請(qǐng)求與POST請(qǐng)求

    2024年02月12日
    瀏覽(25)
  • 爬蟲(chóng)怎么在requests中設(shè)置自己clash軟件的代理ip

    在使用Python的requests庫(kù)時(shí),可以通過(guò)設(shè)置代理來(lái)使用Clash軟件提供的代理IP。以下是設(shè)置代理IP的步驟: 首先,需要確保Clash軟件已經(jīng)安裝并且啟動(dòng),并且在Clash軟件中已經(jīng)添加了需要使用的代理。 然后,在Python中使用requests庫(kù)發(fā)送請(qǐng)求時(shí),可以通過(guò)設(shè)置proxies參數(shù)來(lái)設(shè)置代理。

    2024年02月12日
    瀏覽(22)
  • 小程序開(kāi)發(fā)指南-TS封裝wx.request

    小程序開(kāi)發(fā)指南-TS封裝wx.request

    大家好,我是Aliom252181,一個(gè)佛系且資質(zhì)平平的前端coder,今天分享下我是如何使用 Typescript 封裝 wx.request 的。 本篇文章適合有封裝TS版本小程序請(qǐng)求需求的coder,通過(guò)本篇閱讀,你將會(huì)了解到: TS代碼提示; 單例模式; 每個(gè)接口都可以靈活配置請(qǐng)求頭、超時(shí)時(shí)間等; 取消原

    2024年02月09日
    瀏覽(14)
  • Python+requests+pytest+allure封裝接口自動(dòng)化1-項(xiàng)目結(jié)構(gòu)目錄創(chuàng)建,requests庫(kù)封裝

    Python+requests+pytest+allure封裝接口自動(dòng)化1-項(xiàng)目結(jié)構(gòu)目錄創(chuàng)建,requests庫(kù)封裝

    api: 這是一個(gè)package,用來(lái)各個(gè)接口的類(lèi)封裝,按照你的業(yè)務(wù)可以將其分為多個(gè)package common: 這是一個(gè)package,用來(lái)封裝底層公共方法,比如requests庫(kù)封裝、文件操作封 裝、加解密封裝、redis封裝、數(shù)據(jù)庫(kù)封裝、隨機(jī)數(shù)據(jù)封裝、日志封裝 testcases: 這是一個(gè)package,用來(lái)編寫(xiě)封裝我們

    2024年02月12日
    瀏覽(120)
  • Python接口自動(dòng)化之request請(qǐng)求封裝

    Python接口自動(dòng)化之request請(qǐng)求封裝

    我們?cè)谧鲎詣?dòng)化測(cè)試的時(shí)候,大家都是希望自己寫(xiě)的代碼越簡(jiǎn)潔越好,代碼重復(fù)量越少越好。那么,我們可以考慮將request的請(qǐng)求類(lèi)型(如:Get、Post、Delect請(qǐng)求)都封裝起來(lái)。這樣,我們?cè)诰帉?xiě)用例的時(shí)候就可以直接進(jìn)行請(qǐng)求了。 我們先來(lái)看一下Get、Post、Delect等請(qǐng)求的源碼,

    2024年02月13日
    瀏覽(20)
  • 【Python爬蟲(chóng)】requests庫(kù)

    【Python爬蟲(chóng)】requests庫(kù)

    1.requests庫(kù)的介紹 ? requests 是 Python 語(yǔ)言編寫(xiě),基于 urllib3 ,采用 Apache2 Licensed 開(kāi)源協(xié)議的HTTP庫(kù)。它比 urllib 更加方便,可以節(jié)約我們大量的工作,完全滿足 HTTP 測(cè)試需求。是 Python 實(shí)現(xiàn)的簡(jiǎn)單易用的 HTTP 庫(kù)。 Requests 中文文檔:http://docs.pythonrequests.org/zh_CN/latest/index.html ? 解決

    2024年02月16日
    瀏覽(22)
  • python爬蟲(chóng)—requests

    python爬蟲(chóng)—requests

    類(lèi)型 : models.Response r.text : 獲取網(wǎng)站源碼 r.encoding :訪問(wèn)或定制編碼方式 r.url :獲取請(qǐng)求的 url r.content :響應(yīng)的字節(jié)類(lèi)型 r.status_code :響應(yīng)的狀態(tài)碼 r.headers :響應(yīng)的頭信息 找登錄接口 ?找參數(shù)的值 python代碼 登錄超級(jí)鷹官網(wǎng):超級(jí)鷹驗(yàn)證碼識(shí)別-專(zhuān)業(yè)的驗(yàn)證碼云端識(shí)別服務(wù)

    2024年02月10日
    瀏覽(18)
  • python-網(wǎng)絡(luò)爬蟲(chóng).Request

    python-網(wǎng)絡(luò)爬蟲(chóng).Request

    Request python中requests庫(kù)使用方法詳解: 一簡(jiǎn)介: ????????Requests 是Python語(yǔ)言編寫(xiě),基于urllib, ????????采用Apache2 Licensed開(kāi)源協(xié)議的 HTTP 庫(kù)。 ????????與urllib相比,Requests更加方便,處理URL資源特別流暢。 ????????可以節(jié)約我們大量的工作,建議爬蟲(chóng)使用Requests庫(kù)

    2024年02月14日
    瀏覽(27)
  • Python爬蟲(chóng)之requests模塊

    requests文檔http://docs.python-requests.org/zh_CN/latest/index.html 1、requests模塊的作用: 發(fā)送http請(qǐng)求,獲取響應(yīng)數(shù)據(jù) 2、requests模塊是一個(gè)第三方模塊,需要在你的python(虛擬)環(huán)境中額外安裝 pip/pip3 install requests 3、requests模塊發(fā)送get請(qǐng)求 需求:通過(guò)requests向百度首頁(yè)發(fā)送請(qǐng)求,獲取該頁(yè)面

    2024年02月09日
    瀏覽(18)
  • python爬蟲(chóng)——request模塊講解,Python詳解

    python爬蟲(chóng)——request模塊講解,Python詳解

    對(duì)于GET方式的請(qǐng)求,瀏覽器會(huì)把http header和data一并發(fā)送出去,服務(wù)器響應(yīng)200(返回?cái)?shù)據(jù)); 而對(duì)于POST, 瀏覽器先發(fā)送header,服務(wù)器響應(yīng)100 continue,瀏覽器再發(fā)送data,服務(wù)器響應(yīng)200 ok(返回?cái)?shù)據(jù))。 (二)http常見(jiàn)請(qǐng)求參數(shù) url:請(qǐng)求url地址 headers:請(qǐng)求頭 **data:發(fā)送編碼為表

    2024年04月26日
    瀏覽(18)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包