使用Python请求库发送jquery AJAX GET请求 [英] Sending an jquery AJAX GET request with Python request library
问题描述
我有一个我需要抓取的网站,它正在使用jquery AJAX函数从服务器获取信息.我已经研究了一段时间的代码,并成功使用以下命令从服务器获得响应:
I have a website I need to scrape and it is using jquery AJAX function to get information from the server. I have been investigating the code for a while and I successfully get the response from server using:
data = {'part_number':'1234'}
r = $.ajax({
type : 'GET',
url : 'ajaxurl',
data : data
})
请注意,这是通过js控制台完成的,因此我已经登录.当我尝试在python中进行操作时,我当然需要先登录:
Note that this is done via js console so I am alreadt logged in. When I try to do it in python I need to login first of course:
import requests
headers = {'User-Agent': 'Mozilla/5.0','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
payload = {'username':'me','password':'1234'}
link = 'login url'
session = requests.Session()
resp = session.get(link,headers=headers)
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
resp = session.post(link,headers=headers,data=payload,cookies =cookies)
#until here sucesss!"############
url = "ajaxurl"
my_params={'part_number':'1234'}
r = session.get( url = url, data = my_params, cookies =cookies,headers =headers )
登录后的请求进行得很好,但是对于GET响应,我收到了BAD REQUEST400.我不知道如何格式化请求.我不知道ajax对我的请求有何作用.有人有什么想法吗?
The post request for login goes well but for the GET response I receive BAD REQUEST 400. I cannot figure out how to format my request. I don't know what ajax does to my request. Anyone has any ideas?
预先感谢!
推荐答案
解决了!
我在标题中添加了'X-Requested-With': 'XMLHttpRequest'
并做到了:
Solved it!
I added 'X-Requested-With': 'XMLHttpRequest'
to headers and did:
pn = '1234'
r = requests.get(ajaxurl + '?part_number=' + pn, headers=headers, cookies=cookies)
虽然不明白为什么:(
这篇关于使用Python请求库发送jquery AJAX GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!