简体   繁体   English

如何使用请求捕获发布请求的结果页面?

[英]How can I catch the resulting page of a post request using requests?

I'm using Python with Requests and I'm trying to send data to a form for a website that shortens links. 我正在使用带有请求的Python,我正在尝试将数据发送到缩短链接的网站表单。 I want to store the link to their shortened link in a variable once I use requests.post. 一旦我使用requests.post,我想将链接存储到变量中的缩短链接。

I've read about status codes, but I don't quite know how to used them. 我已经阅读过有关状态代码的内容,但我不太清楚如何使用它们。 Here's what the form for input looks like. 这是输入表单的样子。

<form method="post" action="paste.php">
    <div class="row">
        <div class="12u">
            <input type="text" name="name" id="name" placeholder="Name of paste" />
        </div>
    </div>
    <div class="row">
        <div class="12u">
            <textarea name="paste" id="paste" placeholder="Paste contents"></textarea>
        </div>
    </div>
    <div class="row">
        <div class="12u">
            <a type="submit" class="button form-button-submit">Upload</a>
            <a href="#" class="button button-alt form-button-reset">Undo</a>
        </div>
    </div>
</form>

Here's what I have so far. 这是我到目前为止所拥有的。

def create_paste(url):
    title = 'title' # Define your title.
    paste = 'paste' # Define your paste content.

    try:
        durk = requests.post(url, data={'name': title, 'paste': paste})

        if durk.status_code == 302:
            print('Pasted!')
        else:
            print('Not pasted!')
    except:
        raise

How can I go about doing this? 我该怎么做呢? Thanks. 谢谢。

EDIT: Solved the problem by re-reading the Requests documentation. 编辑:重新阅读请求文档解决了问题。 If I allow redirects and use r.url, I can show the paste link. 如果我允许重定向并使用r.url,我可以显示粘贴链接。

def derp():
    title = 'adsfjkl' # Define your title.
    paste = 'asdfasdhfasdflkjashdfkjasht' # Define your paste content.

    r = requests.post('http://website.com/paste.php', data={'name': title, 'paste': paste}, allow_redirects=True)

    if r.status_code == 200:
        print('Pasted! %s' % (r.url))
    else:
        print('Not pasted!')

    print r.url

The easiest way would be to indeed use Requests.post and then pass this post result to beautifulsoup and parse the result. 最简单的方法是确实使用Requests.post,然后将此帖子结果传递给beautifulsoup并解析结果。

For instance: 例如:

import request
from bs4 import BeautifulSoup as BS

response = request.get('http://stackoverflow.com/') 
bs = BS(response.text)

//do some parsing using beautifulsoup to get the link

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

相关问题 如何将此 requests.post 转换为 locust 请求? - How can I translate this requests.post into a locust request? 如何使用请求库使用参数生成POST请求? - How do I generate a POST request with paramaters using Requests library? 使用 Python 的 requests.post 函数抓取动态 HTML 时,如何指定“页码”? - How I can I specify "page number" when webscraping dynamic HTML using Python's requests.post function? 如何使用 Python 中的会话使用查询字符串发出发布请求? - How can I make post requests with querystrings using sessions in Python? 当页面更改时,即使参数在URL中,我如何使用python请求访问URL? - How can I access a URL using python requests when the page changes when I request it, even though the parameters are in the URL? 如何使用 python 请求库发送此请求 - How can I send this request using python requests library 使用 Python 请求模块向 ASP.NET 页面发送 POST 请求 - POST request to ASP.NET page using the Python requests module 如何在同一页面上处理 POST 请求和打印输出? - How can I handle POST request and print output on the same page? 为什么我不能使用 Python 的 `requests` 库在 POST 请求中将 `None` 作为数据发送? - Why can't I send `None` as data in a POST request using Python's `requests` library? 如何使用python下的请求发出后文件请求? - How can I make post file requests using requests under python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM