鍍金池/ 問(wèn)答/數(shù)據(jù)分析&挖掘  Python/ request請(qǐng)求一個(gè)頁(yè)面在配置過(guò)header之后還是失敗

request請(qǐng)求一個(gè)頁(yè)面在配置過(guò)header之后還是失敗

發(fā)現(xiàn)一個(gè)頁(yè)面用常規(guī)的配置header里面的host,U-A后,依舊不能獲取頁(yè)面數(shù)據(jù).

通過(guò)調(diào)試工具檢查過(guò)發(fā)送的get命令,并沒(méi)有差異。
實(shí)在找不到原因,難道是我缺少那部分的知識(shí)。
求助各位大佬

地址如下:
目標(biāo)采集頁(yè)面

import requests

url_Header = {'Host': 'www.realestate.com.au',
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0'}
url_tar = "https://www.realestate.com.au/property/unit-5-29-stephenson-st-pialba-qld-4655"

r = requests.get(url_tar,headers=url_Header)

if r.status_code == 200:
    print(r.text)
else:
    print(r.status_code)

我這邊采集到的錯(cuò)誤結(jié)果如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="about:blank">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
    <script src="https://resources.kasadapolyform.io/kpfp.js"></script>
    <script src="/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3/fingerprint/script/kpf.js?url=/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3/fingerprint&token=iqyvb0Jj-vnBK-gLSc-eYbn-qSQoY37FIfzn"></script>
</head>
<body>
</body>
</html>

請(qǐng)教是哪種情況我沒(méi)考慮到嗎?
無(wú)需考慮其他xhr內(nèi)容,僅需要Get Hmtl高亮這條命令的內(nèi)容。
只是不知道為什么不能request到同樣的內(nèi)容。
僅需要的Get到html的內(nèi)容

回答
編輯回答
離魂曲

商業(yè)網(wǎng)站怎么可能不防爬,這個(gè)是動(dòng)態(tài)加載的,信息都分塊動(dòng)態(tài)加載啦,你F12捉一下xhr的包看一下,我就看到了幾個(gè)包對(duì)應(yīng)著school啊,Property timeline for 5/29 Stephenson Street啊,Similar homes in Pialba的信息,都是json數(shù)據(jù)看著辣眼,丟到排版器排一下看看吧

圖片描述

如果你只想要那個(gè)包的話就帶上cookie去請(qǐng)求,幾得把cookie轉(zhuǎn)成字典再丟過(guò)去,實(shí)測(cè)成功

import requests
cookie = '*********************'
url = 'https://www.realestate.com.au/property/unit-5-29-stephenson-st-pialba-qld-4655'
headers = {'referer': 'https://www.realestate.com.au/property/unit-5-29-stephenson-st-pialba-qld-4655',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}

def trans_cookie(cookie):
    items =cookie.split(';')
    item_dict = {}
    for item in items:
        key = item.split('=')[0].replace(' ', '')
        value = item.split('=')[1]
        item_dict[key] = value
    print(item_dict)
    return item_dict


cookies = trans_cookie(cookie)





r = requests.get(url,cookies=cookies,headers=headers)
with open('gg.txt','w',encoding='utf-8') as f:
    f.write(r.text)

圖片描述

2017年6月23日 06:27