javascript 使用 Python 请求模拟 ajax POST 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23226074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:34:39  来源:igfitidea点击:

Simulating ajax POST call using Python Requests

javascriptphppythonajax

提问by TomT


I'm doing a project where my parser stealsgets data about every video on the specific site and save it to my database. I have accomplished everything except full link to the video which is hidden.
There is a player, which automaticaly starts on page load. I have found the JavaScript code which starts the player:


我正在做一个项目,我的解析器窃取了特定站点上每个视频的数据并将其保存到我的数据库中。除了隐藏的视频的完整链接之外,我已经完成了所有工作。
有一个播放器,它会在页面加载时自动启动。我找到了启动播放器的 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);
        }
    }  });
}

So after a call if no error found it starts a player using filenamefrom response. That is what I need.
I rechecked a call in Live HTTP Headers:

通话后,因此,如果没有错误发现它开始使用播放器的文件名响应。这就是我需要的。
我重新检查了Live HTTP Headers 中的调用:

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

So it calls ajaxdata.phpwith specific params and in response i should find the filename.
However this Python code returns absolutely nothing to me (neither content nor errors)

所以它用特定的参数调用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

Variable content prints only "Response [200]"
Now I'm completely stuck and would be grateful if anyone could point to errors I done or solutions i could try.

Thanks

可变内容仅打印“响应 [200]”
现在我完全卡住了,如果有人能指出我所做的错误或我可以尝试的解决方案,我将不胜感激。

谢谢

回答by TomT

As Martijn Pieterssuggested, I tried headers one by one and found that this combination is working now:

正如 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)

I thank everyone and especially Martijn Pieters.

我感谢所有人,尤其是Martijn Pieters