【问题标题】:How to simulate HTTP post request using Python Requests module?如何使用 Python Requests 模块模拟 HTTP 发布请求?
【发布时间】:2012-10-20 07:39:29
【问题描述】:

This 是我正在尝试使用的模块,并且我正在尝试自动填写一个表格。我想使用 Requests over Mechanize 的原因是因为使用 Mechanize,我必须先加载登录页面,然后才能填写并提交,而使用 Requests,我可以跳过加载阶段并直接发布消息(希望)。基本上,我试图让登录过程消耗尽可能少的带宽。

我的第二个问题是,在登录过程和重定向之后,是否可以不完全下载整个页面,而只检索页面标题?基本上,只有标题就可以告诉我登录是否成功,所以我想尽量减少带宽使用。

当涉及到 HTTP 请求和诸如此类的东西时,我是一个菜鸟,所以任何帮助将不胜感激。仅供参考,这是一个学校项目。

edit 问题的第一部分已经回答。我现在的问题是第二部分

【问题讨论】:

  • 您可以使用 Chrome 检查器查看将哪些值传递给浏览器创建的发布请求,然后从那里开始。

标签: python forms post python-requests


【解决方案1】:

一些示例代码:

import requests

URL = 'https://www.yourlibrary.ca/account/index.cfm'
payload = {
    'barcode': 'your user name/login',
    'telephone_primary': 'your password',
    'persistent': '1'  # remember me
}

session = requests.session()
r = requests.post(URL, data=payload)
print r.cookies

第一步是查看您的源页面并确定正在提交的 form 元素(使用 Firebug/Chrome/IE 工具(或仅查看源))。然后找到input 元素并确定所需的name 属性(见上文)。

您提供的 URL 恰好有一个“记住我”,虽然我没有尝试过(因为我做不到),但这意味着它会在一段时间内发出一个 cookie 以避免进一步登录——即cookie 保存在request.session

然后只需使用session.get(someurl, ...) 来检索页面等...

【讨论】:

  • 我试过了,但它似乎没有对我进行身份验证,尽管它使用 Mechanize 工作。你知道可能是错的吗? edit 对不起,它确实有效。我只是打错了:)
  • 你是救生员。我以为我必须整天熬过coldfusion废话。最终花了 15 分钟完成 8 小时的手动下载!
  • 那么我如何发送文件?
  • session = requests.session() r = session.post(URL, data=payload) 你声明了session
【解决方案2】:

为了在请求 get 或 post 函数中使用身份验证,您只需提供 auth 参数。像这样:

response = requests.get(url, auth = ('username', 'password')) 有关更多详细信息,请参阅请求Authentication Documentation

使用 Chrome 的开发人员工具,您可以检查 html 页面的元素,其中包含您要填写和提交的表单。有关如何完成此操作的说明,请转到 here。您可以找到填充发布请求的数据参数所需的数据。如果您不担心验证您正在访问的站点的安全证书,那么您也可以在 get 参数列表中指定它。

如果您的 html 页面有这些元素可用于您的网络表单发布:

<textarea id="text" class="wikitext" name="text" cols="80" rows="20">
This is where your edited text will go
</textarea>
<input type="submit" id="save" name="save" value="Submit changes">

那么发布到这个表单的python代码如下:

import requests
from bs4 import BeautifulSoup

url = "http://www.someurl.com"

username = "your_username"
password = "your_password"

response = requests.get(url, auth=(username, password), verify=False)

# Getting the text of the page from the response data       
page = BeautifulSoup(response.text)

# Finding the text contained in a specific element, for instance, the 
# textarea element that contains the area where you would write a forum post
txt = page.find('textarea', id="text").string

# Finding the value of a specific attribute with name = "version" and 
# extracting the contents of the value attribute
tag = page.find('input', attrs = {'name':'version'})
ver = tag['value']

# Changing the text to whatever you want
txt = "Your text here, this will be what is written to the textarea for the post"

# construct the POST request
form_data = {
    'save' : 'Submit changes'
    'text' : txt
} 

post = requests.post(url,auth=(username, password),data=form_data,verify=False)

【讨论】:

    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2014-08-03
    • 1970-01-01
    • 2016-05-17
    • 2023-03-29
    相关资源
    最近更新 更多