python获取ajax的post,使用Python请求模拟ajax POST调用

在解析特定网站视频数据的项目中,通过分析JavaScript代码找到了隐藏的视频链接。利用jQuery的Ajax进行POST请求,目标是/ajaxdata.php。在Live HTTP Headers中观察到请求头和参数。尝试用Python的requests库模拟POST请求,起初未成功,但添加了正确的请求头后,如User-Agent、Accept等,成功获取了内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm doing a project where my parser steals gets 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:

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 filename from response. That is what I need.

I rechecked a call in 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.php with specific params and in response i should find the filename.

However this Python code returns absolutely nothing to me (neither content nor errors)

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

解决方案

As

Martijn Pieters

suggested, I tried headers one by one and found that this combination is working now:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值