簡體   English   中英

使用Python請求模擬Ajax POST調用

[英]Simulating ajax POST call using Python Requests


我正在做一個項目,其中我的解析器 竊取了 有關特定站點上每個視頻的數據,並將其保存到我的數據庫中。 除了指向隱藏視頻的完整鏈接之外,我已完成所有工作。
有一個播放器,它會自動從頁面加載開始。 我發現啟動播放器的JavaScript代碼:

function getVidData(resolution, init) {
    << some code here >>
    jQuery.ajax({type: 'POST', url: '/ajaxdata.php', dataType: 'json', data: 'mod=videodata&vid=48902&res=' + resolution, success: function (response) {
        if (response.error != '' && response.error != undefined) {
        << error handling code here >>
        } else {
            StartPlayer(response.width, response.height, response.filename);
        }
    }  });
}

因此,在通話后,如果沒有發現錯誤,它將使用response中的 filename啟動播放器。 那就是我所需要的。
我重新檢查了Live HTTP標頭中的呼叫:

http://<< SITE_URL >>/ajaxdata.php
POST /ajaxdata.php HTTP/1.1
Host: << SITE_URL >>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: << VIDEO_PAGE >>
Content-Length: 31
Cookie: << COOKIE VALUES >>
DNT: 1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
    mod=videodata&vid=48901&res=640

HTTP/1.1 200 OK
Server: nginx/1.5.9
Date: Tue, 22 Apr 2014 16:30:06 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Tue, 22 Apr 2014 16:30:05 GMT
Cache-Control: no-cache
Pragma: no-cache
Content-Encoding: gzip

因此它使用特定的參數調用ajaxdata.php ,作為回應,我應該找到文件名。
但是,此Python代碼對我絕對沒有任何回報(內容或錯誤)

import requests

url = "http://LALLALAA/ajaxdata.php"
data_video = {"mod": "videodata", "vid": "48901", 'res': '640'}

s = requests.Session()
s.post(login_url, data=login_data) # Authentication

content = s.post(url, data=data_video)
print content.content

可變內容僅打印“響應[200]”
現在,我完全陷入困境,如果有人可以指出我所做的錯誤或我可以嘗試的解決方案,我將不勝感激。

謝謝

正如Martijn Pieters所建議的那樣,我一個接一個地嘗試了標頭,發現此組合現在可以使用:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest'
}

s = requests.Session()
s.post(login_url, data=login_data)

content = s.post(url, data=data_video, headers=headers)

我感謝所有人,特別是Martijn Pieters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM