python发起ajax请求_使用Python请求库发送jquery AJAX GET请求
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 serve

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
})
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 )
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?
Thanks in Advance!
解决方案
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)
Did not understand why though :(
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)