python3 requests.post接口抓取實例

以下是使用requests,抓取發過站的的top排名和recent評論的api[post] 的post實例

具體接口分析,自行打開amazon.fr分析

def api_fr(asin): #orderBY[recent,helpful]
    for msg in [['recent',"reviewsAjax3"],['helpful',"reviewsAjax2"]]:
        if msg[0]=='recent':
            status='most 評分'
        else:
            status='top 評分'
        print("當前模式是:%s"%status)
        headers_fr = {
            "Accept": "text/html,*/*",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Origin": "https://www.amazon.fr",
            "Referer": "https://www.amazon.fr/dp/%s?th=1"%asin,
            "Sec-Fetch-Mode": "cors",
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }
        data={
            "asin": asin,
            "sortBy": msg[0],       #helpful&reviewsAjax2
            "scope": msg[1]
        }
        r=requests.post(url_fr,data=data,headers=headers_fr)
        r.encoding='utf-8'
        json_text=r.text
        myLIST=json_text.split("&&&")
        for msg in myLIST[2:-5]:
            item=msg.replace("\n","")
            # 得到節點
            html=list(eval(item))[2]
            html_x = etree.HTML(html)
            try:
                # 獲得評分
                pf= html_x.xpath("//span[@class='a-icon-alt']/text()")[0].split(" ")[0]
                # 評論者的用戶名
                pr_name=html_x.xpath("//span[@class='a-profile-name']/text()")[0]
                print("    ",pr_name,"給出的評分是",pf)
            except:
                pass

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章