简体   繁体   English

使用 requests 模块模拟 HTTP 发布请求不起作用

[英]Simulating HTTP post request using requests module is not working

I tried to retrieve a page after submitting the form in the following link (with default options):https://selfservice.pasadena.edu/prod/pw_psearch_sched.p_search在以下链接(使用默认选项)提交表单后,我尝试检索页面:https://selfservice.pasadena.edu/prod/pw_psearch_sched.p_search

I realized that the form is post method, so I used requests module to simulate a post request.我意识到表单是post方法,所以我使用requests模块来模拟一个post请求。

r = requests.post(URL, data=form_data).text

For the form data, I extracted using chrome developer tool.对于表单数据,我使用 chrome 开发工具提取。

form_data = {
    'TERM': '202070',
    'TERM_DESC': 'Fall 2020',
    'sel_subj': 'dummy',
    'sel_day': 'dummy',
    'sel_schd': 'dummy',
    'sel_camp': 'dummy',
    'sel_ism': 'dummy',
    'sel_sess': 'dummy',
    'sel_instr': 'dummy',
    'sel_ptrm': 'dummy',
    'sel_attrib': 'dummy',
    'being_hh': '5',
    'begin_mi': '0',
    'begin_ap': 'a',
    'end_hh': '11',
    'end_mi': '0',
    'end_ap': 'p',
    'aa': 'N',
    'bb': 'N',
    'ee': 'N'
}

But it keeps giving me 404 Error .但它一直给我404 Error

At first, I tried to use selenium to simulate clicking the submit button but it is way too slow.起初,我尝试使用selenium来模拟点击提交按钮,但速度太慢了。

Any help would be appreciated.任何帮助,将不胜感激。

There are two problems有两个问题

  1. form sends to different url: https://selfservice.pasadena.edu/prod/pw_psearch_sched.p_listthislist表格发送到不同的 url: https://selfservice.pasadena.edu/prod/pw_psearch_sched.p_listthislist

  2. small mistake in name 'begin_hh' - you have 'being_hh'名称'begin_hh'中的小错误 - 你有'being_hh'


Working code工作代码

import requests

data = {
    'TERM': '202070',
    'TERM_DESC': 'Fall 2020',
    'sel_subj':   'dummy',
    'sel_day':    'dummy',
    'sel_schd':   'dummy',
    'sel_camp':   'dummy',
    'sel_ism':    'dummy',
    'sel_sess':   'dummy',
    'sel_instr':  'dummy',
    'sel_ptrm':   'dummy',
    'sel_attrib': 'dummy',

    'sel_crse': '',
    'sel_crn': '',
    'sel_title': '',

    'begin_hh': '5',
    'begin_mi': '0',
    'begin_ap': 'a',
    'end_hh': '11',
    'end_mi': '0',
    'end_ap': 'p',
    'aa': 'N',
    'bb': 'N',
    'ee': 'N',
}

URL = 'https://selfservice.pasadena.edu/prod/pw_psearch_sched.p_listthislist'
r = requests.post(URL, data=data)
print(r.status_code)
#print(r.text)

BTW: originally form sends two values for some fields and it would be顺便说一句:最初表单为某些字段发送两个值,它将是

'sel_subj':   ['dummy', '%'],
'sel_day':    'dummy',
'sel_schd':   ['dummy', '%'],
'sel_camp':   ['dummy', '%'],
'sel_ism':    ['dummy', '%'],
'sel_sess':   ['dummy', '%'],
'sel_instr':  ['dummy', '%'],
'sel_ptrm':   ['dummy', '%'],
'sel_attrib': ['dummy', '%'],

but page works even if you send one value.但是即使您发送一个值,页面也可以工作。


BTW: in DevTool you can get code for rpogram curl and later you can convert it to Python code using page https://curl.trillworks.com/顺便说一句:DevTool中,您可以获得 rpogram curl的代码,稍后您可以使用页面https://curl.com将其转换为 Python 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python Requests模块模拟HTTP发布请求? - How to simulate HTTP post request using Python Requests module? Python'请求'模块中的POST请求无法正常工作 - POST Request in Python 'requests' module not working properly 有没有办法使用 python requests 模块形成批处理请求? 我想在单个 POST 请求中发送多个同质 http API 请求 - Is there a way to form batched requests using python requests module ? I want to send multiple homogenous http API requests in a single POST request 模拟HTTP POST请求会产生意外结果 - Simulating HTTP POST request produces unexpected result 使用模块请求发送带有有效负载的POST请求 - Sending POST request with payload using a module requests 使用请求模块在Python中的Http Post请求中超出了最大重试次数并且证书验证失败 - Max retries exceeded and Certificate Verify Failed in Http Post Request in Python using requests Module 如何通过 http 使用 requests 模块和 Z9784E21C7B2AFDD678Z87 发布请求发送 numpy 数组或 pytorch 张量 - how to send an numpy array or a pytorch Tensor through http post request using requests module and Flask 使用Python请求模拟Ajax POST调用 - Simulating ajax POST call using Python Requests 使用请求lib使用python模拟ajax请求 - Simulating ajax request with python using requests lib 使用请求库使用python模拟ajax请求 - Simulating ajax request with python using the requests lib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM