站長資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

芯片大家都不陌生。在當(dāng)今疫情下,顯卡,車機(jī)的芯片產(chǎn)量銳減影響了不少人的購物需求(反正你也買不到),也讓不少人重新認(rèn)識(shí)了半導(dǎo)體行業(yè)。閑來無事,我們可以獲取一下T網(wǎng)站的芯片庫存和芯片信息。

一、列表頁請求分析

進(jìn)入頁面,就能看到我們需求的信息了。

但是,在頁面請求完成之前,有一點(diǎn)點(diǎn)不對勁,就是頁面的各個(gè)部份請求的速度是不一樣的:

所以啊,需要的數(shù)據(jù),大概率不是簡單的get請求,所以要進(jìn)一步去看,特意在開發(fā)者模式—Fetch/XHR選項(xiàng)卡中有一個(gè)請求,返回值正好是我們需要的內(nèi)容:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

程序員必備接口測試調(diào)試工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api設(shè)計(jì)、調(diào)試、文檔、自動(dòng)化測試工具
后端、前端、測試,同時(shí)在線協(xié)作,內(nèi)容實(shí)時(shí)同步

這一條鏈接返回了所有的數(shù)據(jù),無需翻頁,下面開始請求鏈接。

二、列表頁請求

根據(jù)上面的鏈接,直接get請求,分析json即可,上代碼:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

 def getItemList():        url = "https://www.xx.com.cn/selectiontool/paramdata/family/3658/results?lang=cn&output=json"        headers = {            'authority': 'www.xx.com.cn',            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",        }        res = getRes(url,headers,'','','GET')//自己寫的請求方法        nodes = res.json()['ParametricResults']        for node in nodes:            data = {}            data["itemName"] = node["o3"] #名稱            data["inventory"] = node["p3318"] #庫存            data["price"] = node["p1130"]['multipair1']['l'] #價(jià)格            data["infoUrl"] = f"https://www.xx.com.cn/product/cn/{node['o1']}"#詳情URL
登錄后復(fù)制

分析上面的json,可知 o3 是商品名,p3318是庫存,p1130里面的內(nèi)容有一個(gè)帶單位的價(jià)格,o1是型號(hào),可湊出詳情鏈接,下面是請求結(jié)果:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

三、詳情頁分析

終于拿到詳情頁鏈接了,該獲取剩下的內(nèi)容了。

打開開發(fā)者模式,沒有額外的請求,只有一個(gè)包含內(nèi)容的get請求。

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

那直接請求不就得了,上代碼:

def getItemInfo(url):       logger.info(f'正在請求詳情url-{url}')       headers = {           'authority': 'www.xx.com.cn',           'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",           'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",           'referer':'https://www.xx.com.cn/product/cn/THS4541-DIE',           }        res = getRes(url, headers,'', '', 'GET')//自己寫的請求方法        content = res.content.decode('utf-8')
登錄后復(fù)制

但是發(fā)現(xiàn),請求的詳情頁,跟開發(fā)者模式的預(yù)覽怎么不太一樣?

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

我這里的第一反應(yīng)就覺得,完了,這個(gè)需要cookie。

繼續(xù)分析,清屏開發(fā)者模式,清除cookie,再次訪問詳情鏈接,在All選項(xiàng)卡中,可以發(fā)現(xiàn):

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

本以為該請求一次的詳情頁鏈接請求了兩次,兩次中間還有一個(gè)xhr請求。

預(yù)覽第一次請求,可以發(fā)現(xiàn)跟剛才本地請求的內(nèi)容相差無幾:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

所以問題出在第二次的請求,進(jìn)一步分析:

查看第二次的get請求,與第一次的請求相差了一堆cookie

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

簡化cookie,發(fā)現(xiàn)這些cookie最關(guān)鍵的參數(shù)是ak_bmsc這一部分,而這一部分參數(shù),就來自上一個(gè)xhr請求中的響應(yīng)頭set-cookie中:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

分析這個(gè)xhr請求,請求鏈接

這是個(gè)post請求,先從payload參數(shù)下手:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

這個(gè)bm-verify參數(shù),是不是有些眼熟?這就是第一次的get請求返回的內(nèi)容嗎,下面還有一個(gè)pow參數(shù):

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

"pow":j,這個(gè)j參數(shù)就在上面,聲明了i和兩個(gè)拼接的數(shù)字字符串轉(zhuǎn)成int之后相加之后的結(jié)果:

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

通過這一系列請求,返回了最終get請求所需要的cookie,講的比較瑣碎,上代碼:

 #詳情需要cookie    def getVerify(url):        infourl = url        headers = {            'authority': 'www.xx.com.cn',            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",        }        proxies = getApiIp()//取代理        if proxies:            #無cookie訪問詳情頁拿參數(shù)bm-verify,pow            res = getRes(infourl,headers,proxies,'','GET')            if res:                #拿第一次請求的ak_bmsc                cookie = re.findall("ak_bmsc=.*?;",res.headers['set-cookie'])[0]                #拿bm-verify                verifys = re.findall('"bm-verify": "(.*?)"', res.text)[0]                #合并字符串轉(zhuǎn)int相加取pow                a = re.findall('var i = (d+);',res.text)[0]                b = re.findall('Number("(.*?)");',res.text)[0]                b = int(b.replace('" + "',''))                pow = int(a)+b                post_data = {                    'bm-verify': verifys,                    'pow':pow                }                #轉(zhuǎn)json                post_data = json.dumps(post_data)                if verifys:                    logger.info('第一次參數(shù)獲取完畢')                    return post_data,proxies,cookie                else:                    print('verify獲取異常')            else:                print('verify請求出錯(cuò)')         # 第二次帶參數(shù)訪問驗(yàn)證鏈接    def getCookie(url):        post_headers = {            "authority": "www.xx.com.cn",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",            "accept": "*/*",            "content-type": "application/json",            "origin": "https://www.xx.com.cn",            "referer":url,        }        post_data,proxies,c_cookie = getVerify(url)        post_headers['Cookie'] = c_cookie        posturl = "https://www.xx.com.cn/_sec/verify?provider=interstitial"        check = getRes(posturl,post_headers,proxies,post_data,'POST')        if check:        #從請求頭拿到ak_bmsc cookie            cookie = check.headers['Set-Cookie']            cookie = re.findall("ak_bmsc=.*?;",cookie)[0]            if cookie:                logger.info('Cookie獲取完畢')                return cookie,proxies            else:                print('cookie獲取異常')        else:            print('cookie請求出錯(cuò)')
登錄后復(fù)制

簡單的概括一下詳情頁的請求流程:

第一次請求,取得所需參數(shù)bm-verify,pow,cookie,提供給下一次的post請求(getVerify方法)

第二次請求,根據(jù)已知條件進(jìn)行post請求,并獲取響應(yīng)頭cookie的ak_bmsc(getCookie)

切記,在整個(gè)獲取cookie的三次請求過程中,第二、三兩次請求都需要伴隨著上一次請求的ak_bmsc作為cookie傳遞,第二次請求需要第一次的ak_bmsc,最終請求需要第二次的ak_bmsc。

四、詳情頁請求

 def getItemInfo(url):        logger.info(f'正在請求詳情url-{url}')        cookie,proxies = getCookie(url)        headers = {            'authority': 'www.xx.com.cn',            'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",            'referer':'https://www.xx.com.cn/product/cn/THS4541-DIE',            'cookie':cookie        }        res = getRes(url, headers,proxies, '', 'GET')        content = res.content.decode('utf-8')        print(content)        exit()        sel = Selector(text=content)        Parameters = sel.xpath('//ti-tab-panel[@tab-title="參數(shù)"]/ti-view-more/div').extract_first()        Features = sel.xpath('//ti-tab-panel[@tab-title="特性"]/ti-view-more/div').extract_first()        Description = sel.xpath('//ti-tab-panel[@tab-title="描述"]/ti-view-more').extract_first()        if Parameters and Features and Description:            return Parameters,Features,Description
登錄后復(fù)制

通過上一步cookie的獲取,帶著cookie再次訪問詳情鏈接,就可以順利的獲取內(nèi)容并可以使用xpath進(jìn)行解析,獲取需要的內(nèi)容。

五、代理設(shè)置

T網(wǎng)站詳情頁帶cookie請求有100多次,如果用本地代理一直去請求,會(huì)有IP封鎖的可能性出現(xiàn),導(dǎo)致無法正常獲取。所以,需要高效請求的話,優(yōu)質(zhì)穩(wěn)定的代理IP必不可少,我這里使用的ipidea代理請求的T網(wǎng)站,數(shù)據(jù)很快就訪問出來了。

地址:http://www.ipidea.net/?utm-source=csdn&utm-keyword=?wb ,首次可以白嫖流量哦。本次使用的api獲取,代碼如下:

 # api獲取ip    def getApiIp():        # 獲取且僅獲取一個(gè)ip        api_url = 'http://tiqu.ipidea.io:81/abroad?num=1&type=2&lb=1&sb=0&flow=1?ions=&port=1'        res = requests.get(api_url, timeout=5)        try:            if res.status_code == 200:                api_data = res.json()['data'][0]                proxies = {                    'http': 'http://{}:{}'.format(api_data['ip'], api_data['port']),                    'https': 'http://{}:{}'.format(api_data['ip'], api_data['port']),                }                print(proxies)                return proxies            else:                print('獲取失敗')        except:            print('獲取失敗')
登錄后復(fù)制

六、代碼匯總

 # coding=utf-8    import requests    from scrapy import Selector    import re    import json    from loguru import logger         # api獲取ip    def getApiIp():        # 獲取且僅獲取一個(gè)ip        api_url = '獲取代理地址'        res = requests.get(api_url, timeout=5)        try:            if res.status_code == 200:                api_data = res.json()['data'][0]                proxies = {                    'http': 'http://{}:{}'.format(api_data['ip'], api_data['port']),                    'https': 'http://{}:{}'.format(api_data['ip'], api_data['port']),                }                print(proxies)                return proxies            else:                print('獲取失敗')        except:            print('獲取失敗')         def getItemList():        url = "https://www.xx.com.cn/selectiontool/paramdata/family/3658/results?lang=cn&output=json"        headers = {            'authority': 'www.xx.com.cn',            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",        }        proxies = getApiIp()        if proxies:            # res = requests.get(url, headers=headers, proxies=proxies)            res = getRes(url,headers,proxies,'','GET')            nodes = res.json()['ParametricResults']            for node in nodes:                data = {}                data["itemName"] = node["o3"] #名稱                data["inventory"] = node["p3318"] #庫存                data["price"] = node["p1130"]['multipair1']['l'] #價(jià)格                data["infoUrl"] = f"https://www.ti.com.cn/product/cn/{node['o1']}"#詳情URL                Parameters, Features, Description = getItemInfo(data["infoUrl"])                data['Parameters'] = Parameters                data['Features'] = Features                data['Description'] = Description                print(data)         #詳情需要cookie    def getVerify(url):        infourl = url        headers = {            'authority': 'www.xx.com.cn',            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",        }        proxies = getApiIp()        if proxies:            #訪問詳情頁拿參數(shù)bm-verify,pow            res = getRes(infourl,headers,proxies,'','GET')            if res:                #拿第一次請求的ak_bmsc                cookie = re.findall("ak_bmsc=.*?;",res.headers['set-cookie'])[0]                #拿bm-verify                verifys = re.findall('"bm-verify": "(.*?)"', res.text)[0]                #字符串轉(zhuǎn)int相加取pow                a = re.findall('var i = (d+);',res.text)[0]                b = re.findall('Number("(.*?)");',res.text)[0]                b = int(b.replace('" + "',''))                pow = int(a)+b                post_data = {                    'bm-verify': verifys,                    'pow':pow                }                #轉(zhuǎn)json                post_data = json.dumps(post_data)                if verifys:                    logger.info('第一次參數(shù)獲取完畢')                    return post_data,proxies,cookie                else:                    print('verify獲取異常')            else:                print('verify請求出錯(cuò)')         # 第二次帶參數(shù)訪問驗(yàn)證鏈接    def getCookie(url):        post_headers = {            "authority": "www.xx.com.cn",            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",            "accept": "*/*",            "content-type": "application/json",            "origin": "https://www.xx.com.cn",            "referer":url,        }        post_data,proxies,c_cookie = getVerify(url)        post_headers['Cookie'] = c_cookie        posturl = "https://www.xx.com.cn/_sec/verify?provider=interstitial"        check = getRes(posturl,post_headers,proxies,post_data,'POST')        if check:        #從請求頭拿到ak_bmsc cookie            cookie = check.headers['Set-Cookie']            cookie = re.findall("ak_bmsc=.*?;",cookie)[0]            if cookie:                logger.info('Cookie獲取完畢')                return cookie,proxies            else:                print('cookie獲取異常')        else:            print('cookie請求出錯(cuò)')         def getItemInfo(url):        logger.info(f'正在請求詳情url-{url}')        cookie,proxies = getCookie(url)        headers = {            'authority': 'www.xx.com.cn',            'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",            'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",            'referer':'https://www.xx.com.cn/product/cn/THS4541-DIE',            'cookie':cookie        }        res = getRes(url, headers,proxies, '', 'GET')        content = res.content.decode('utf-8')        sel = Selector(text=content)        Parameters = sel.xpath('//ti-tab-panel[@tab-title="參數(shù)"]/ti-view-more/div').extract_first()        Features = sel.xpath('//ti-tab-panel[@tab-title="特性"]/ti-view-more/div').extract_first()        Description = sel.xpath('//ti-tab-panel[@tab-title="描述"]/ti-view-more').extract_first()        if Parameters and Features and Description:            return Parameters,Features,Description         #專門發(fā)送請求的方法,代理請求三次,三次失敗返回錯(cuò)誤    def getRes(url,headers,proxies,post_data,method):        if proxies:            for i in range(3):                try:                    # 傳代理的post請求                    if method == 'POST':                        res = requests.post(url,headers=headers,data=post_data,proxies=proxies)                    # 傳代理的get請求                    else:                        res = requests.get(url, headers=headers,proxies=proxies)                    if res:                        return res                except:                    print(f'第{i}次請求出錯(cuò)')                else:                    return None        else:            for i in range(3):                proxies = getApiIp()                try:                    # 請求代理的post請求                    if method == 'POST':                        res = requests.post(url, headers=headers, data=post_data, proxies=proxies)                    # 請求代理的get請求                    else:                        res = requests.get(url, headers=headers, proxies=proxies)                    if res:                        return res                except:                    print(f"第{i}次請求出錯(cuò)")                else:                    return None         if __name__ == '__main__':       getItemList()
登錄后復(fù)制

基于Python通過cookie對某芯片網(wǎng)站信息的獲取

通過上述步驟,已經(jīng)能獲取所需內(nèi)容。

總結(jié)

整個(gè)T網(wǎng)站的數(shù)據(jù)獲取,難點(diǎn)就在詳情頁的cookie,(其實(shí)也不是很難,只不過cookie太長比較費(fèi)眼)理順了整個(gè)請求流程,剩下的就是請求的過程。穩(wěn)定高效的IP代理會(huì)讓你事半功倍,通過api獲取可變的代理也不易被網(wǎng)站封禁,從而更好地獲取數(shù)據(jù)。簡化cookie的時(shí)候使用合適的請求工具會(huì)更方便,比如postman,burp。

這次的整個(gè)流程到此結(jié)束,講的比較啰嗦,若有錯(cuò)誤或者更好的方法請大佬指正!

贊(0)
分享到: 更多 (0)
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
日韩精品在线一区二区| 国产99视频精品专区| 精品久久久久久久久中文字幕| 日本精品久久久久影院日本| 国产成人一区二区精品非洲| 在线精品免费视频无码的| 97热久久免费频精品99| 亚洲国产精品成人久久久| 老司机亚洲精品影院无码| 久久这里只精品热免费99| 亚洲中文字幕无码久久精品1 | 亚洲精品色婷婷在线影院| 亚洲国产精品日韩| 国产成人无码aa精品一区| 精品久久久中文字幕二区| 欧美精品久久久久a片一二三区| 2021国产精品午夜久久| 国产精品久久精品| 国产精品九九九久久九九| 国产高清国产精品国产专区| 久久久久亚洲精品日久生情| 精品亚洲麻豆1区2区3区| 人妻熟妇乱又伦精品视频| 久久9精品久久久| 四虎国产精品永久在线观看| 久久久精品波多野结衣AV| 午夜不卡久久精品无码免费| 久久九九久精品国产日韩经典| 久久精品国产精品亚洲色婷婷| 日韩欧国产精品一区综合无码| 日韩精品一区二区三区四区| 国产成人久久精品激情 | 99精品人妻无码专区在线视频区| 亚洲偷自精品三十六区| 国产精品久久久久天天影视| 69国产成人综合久久精品| 国产精品99久久久久久宅男小说| 中文字幕一区日韩精品| 含羞草国产亚洲精品岁国产精品| 国产99视频精品专区| 国产精品午夜爆乳美女视频|