首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Python请求模拟Ajax POST调用?

如何使用Python请求模拟Ajax POST调用?
EN

Stack Overflow用户
提问于 2018-06-13 06:03:13
回答 1查看 0关注 0票数 0

我在做一个解析器的项目获取有关特定站点上的每个视频的数据,并将其保存到我的数据库中。我已经完成了所有的事情,除了完全链接到视频,这是隐藏的。

有一个播放器,自动启动页面加载。我找到了启动播放器的JavaScript代码:

代码语言:txt
复制
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);
        }
    }  });
}
代码语言:txt
复制
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
代码语言:txt
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-13 15:45:38

代码语言:txt
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档