简体   繁体   English

使用Requests Python库形成讨厌的POST请求

[英]Forming a nasty POST request using Requests Python library

I am working on a Python script that automatically helps me add classes in my school's add-a-class web interface. 我正在研究一个Python脚本,该脚本可自动帮助我在学校的添加类网络界面中添加类。 What I've done so far is, I am able to be authenticated into the website and access the HTML of this add-a-class page. 到目前为止,我已经可以通过网站验证并访问此add-a-class页面的HTML。 The interface works like so: 该界面的工作方式如下:

在此处输入图片说明

The items under Status indicate classes I am currently taking this term. Status下的项目表示我目前正在上这学期的课程。

Under "Add Classes Worksheet", you have a few input 5 digit number called a CRN ie 34688 . 在“添加类工作表”下,您有几个输入的5位数字,称为CRN34688 You can type in 10 CRNs at a time. 您一次可以输入10个CRN。

I was thinking I could simply query the URL with the CRNs that I am interested in adding. 我当时想我可以简单地用我有兴趣添加的CRN查询URL。 Thinking it would be simple, I opened up devtools, and it turns out the POST is a total mess. 以为这很简单,我打开了devtools,结果发现POST完全是一团糟。

Here is the raw form data that I get from the URL it queries under Chrome Dev Tools when I try to add a class in the "Add Classes Worksheet" 这是当我尝试在“添加类工作表”中添加类时从Chrome开发工具下查询的URL获取的原始表单数据

term_in=201535&RSTS_IN=DUMMY&assoc_term_in=DUMMY&CRN_IN=DUMMY&start_date_in=DUMMY&end_date_in=DUMMY&SUBJ=DUMMY&CRSE=DUMMY&SEC=DUMMY&LEVL=DUMMY&CRED=DUMMY&GMOD=DUMMY&TITLE=DUMMY&MESG=DUMMY&REG_BTN=DUMMY&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=34688&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=ECEC&CRSE=451&SEC=001&LEVL=Undergraduate+Quarter&CRED=++++3.000&GMOD=Standard+Letter&TITLE=Computer+Arithmetic&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=31109&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=BIO&CRSE=141&SEC=073&LEVL=Undergraduate+Quarter&CRED=++++0.000&GMOD=Non+Gradeable+Unit&TITLE=Essential+Biology&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=31099&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=BIO&CRSE=141&SEC=B&LEVL=Undergraduate+Quarter&CRED=++++4.500&GMOD=Standard+Letter&TITLE=Essential+Biology&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=30091&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=ECE&CRSE=493&SEC=001&LEVL=Undergraduate+Quarter&CRED=++++4.000&GMOD=Standard+Letter&TITLE=Senior+Design+Project+III&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=31117&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=BIO&CRSE=141&SEC=001&LEVL=Undergraduate+Quarter&CRED=++++0.000&GMOD=Non+Gradeable+Unit&TITLE=Essential+Biology&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=31525&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=BIO&CRSE=141&SEC=072&LEVL=Undergraduate+Quarter&CRED=++++0.000&GMOD=Non+Gradeable+Unit&TITLE=Essential+Biology&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=30013&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=EXAM&CRSE=080&SEC=001&LEVL=Undergraduate+Quarter&CRED=++++0.000&GMOD=Non+Gradeable+Unit&TITLE=Common+Exam+Period+-+I&MESG=DUMMY&RSTS_IN=&assoc_term_in=201535&CRN_IN=34039&start_date_in=03%2F28%2F2016&end_date_in=06%2F11%2F2016&SUBJ=ENTP&CRSE=205&SEC=001&LEVL=Undergraduate+Quarter&CRED=++++3.000&GMOD=Standard+Letter&TITLE=Ready%2C+Set%2C+Fail&RSTS_IN=WR&CRN_IN=34889&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&RSTS_IN=WR&CRN_IN=&assoc_term_in=&start_date_in=&end_date_in=&regs_row=8&wait_row=0&add_row=10&REG_BTN=Submit+Changes

From what it appears, it seems in the request, not only do you add the new CRNs that you want to add, the classes you have added so far also becomes part of the new request. 从外观上看,它似乎在请求中,您不仅添加了要添加的 CRN,到目前为止添加的类成为了新请求的一部分。

Looking deeper into the request, it seems each class, whether it's already a class you currently have in your schedule or it's a class you are adding via the textbox, it follows the following format: 深入研究请求,似乎每个类,无论它已经是您计划中当前拥有的类,还是正在通过文本框添加的类,都遵循以下格式:

assoc_term_in=201535
CRN_IN=34688
start_date_in=03%2F28%2F2016
end_date_in=06%2F11%2F2016
SUBJ=ECEC
CRSE=451
SEC=001
LEVL=Undergraduate+Quarter
CRED=++++3.000
GMOD=Standard+Letter
TITLE=Computer+Arithmetic
MESG=DUMMY
RSTS_IN=

Each class has 13 associated query keys and values. 每个类具有13个关联的查询键和值。 I am actually not sure if it's required to re-submit classes that you've already added, and I am willing to perform some tests if necessary. 我实际上不确定是否需要重新提交您已经添加的类,并且我愿意在必要时执行一些测试。

The request itself is a POST: 该请求本身是一个POST:

Request URL:https://school.edu/pls/duprod/bwckcoms.P_Regs 
Request Method:POST 
Status Code:200 OK 
Remote Address:[some IP address]

My question is: 我的问题是:

  1. Can I submit CRNs directly to the textbox avoiding having the re-form the nasty POST request? 我是否可以将CRN直接提交到文本框,而不必重新设置讨厌的POST请求? (ie some Python library that does so). (即某些这样做的Python库)。 Seems like Mechanize is an option, but is it possible to use Requests with Mechanize? 似乎可以选择“机械化”,但是可以将“请求”与“机械化”一起使用吗? I prefer not to re-write all the authentication it took to get into that form page. 我宁愿不重写进入该表单页面所需的所有身份验证。

  2. If the above option is not possible, any ideas how I can re-form the POST request efficiently? 如果上述选项不可行,那么有什么想法可以有效地重新构成POST请求?

One thing you could do is to use a dict in order to hold all of the information for the post request per Requests documentation . 您可以做的一件事是使用dict来保存每个Requests文档中有关过帐请求的所有信息。 For example your code may look like: 例如,您的代码可能如下所示:

import json
import requests

payload = {
  "assoc_term_in":"201535",
  "CRN_IN":"34688",
  "start_date_in":"03%2F28%2F2016",
  "end_date_in":"06%2F11%2F2016",
  "SUBJ":"ECEC",
  "CRSE":"451",
  "SEC":"001",
  "LEVL":"Undergraduate+Quarter",
  "CRED":"++++3.000",
  "GMOD":"Standard+Letter",
  "TITLE":"Computer+Arithmetic",
  "MESG":"DUMMY",
  "RSTS_IN":""
}

r = requests.post("https://school.edu/pls/duprod/bwckcoms.P_Regs", data=json.dumps(payload))
print(r.text)

Then you can change any of the values that you need to as you would with a normal dictionary. 然后,您可以像使用普通词典一样更改所需的任何值。 If you wanted to go with #1, one of your options would be to look into something like Selenium , however, I personally feel if you already know what the POST request looks like doing something above may be easier. 如果您想使用#1,您的选择之一就是研究Selenium之类的东西,但是,我个人认为,如果您已经知道POST请求看起来像做上面的事情,则可能会更容易。

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

相关问题 Python请求:1个单个请求中的POST JSON和文件(多种数据) - Python Requests: POST JSON and file (multiform data) in 1 single request 带有Python模块的POST请求 - POST requests with Python module 使用 javax 库从 POST 请求获取文件 - get file from POST request using javax library 使用POST请求和Java客户端库加载到BigQuery的任何示例? - Any examples of loading to BigQuery using a POST request and Java client library? 使用 GSON 形成 Json - Forming Json using GSON Python 请求模拟 CURL POST 发送带有 1 个或多个文件和 JSON 正文的多部分请求 - Python Requests Emulate a CURL POST sending multipart request with 1 or more files AND JSON body 使用android volley库发送JSONArray POST请求 - Send a JSONArray POST request with android volley library Volley 库在发布请求时给出错误 500 - Volley Library giving error 500 on post request 如何使用Java使用正确的实体构建http post请求而不使用任何库? - How to build a http post request with the correct entity with Java and not using any library? 有没有办法在没有外部库的情况下通过NetBeans发送GET和POST请求? - Is there a way to send GET and POST requests via NetBeans without an external library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM