[英]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.