热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Python登录网站,填写表格,然后退出-UsingPythontosignintowebsite,fillinaform,thensignout

AspartofmyquesttobecomebetteratPythonIamnowattemptingtosignintoawebsiteIfrequen

As part of my quest to become better at Python I am now attempting to sign in to a website I frequent, send myself a private message, and then sign out. So far, I've managed to sign in (using urllib, COOKIEjar and urllib2). However, I cannot work out how to fill in the required form to send myself a message.

作为我在Python上变得更好的一部分,我现在正试图登录我经常访问的网站,给自己发一条私信,然后退出。到目前为止,我已设法登录(使用urllib,COOKIEjar和urllib2)。但是,我无法弄清楚如何填写所需的表格来向自己发送消息。

The form is located at /messages.php?action=send. There's three things that need to be filled for the message to send: three text fields named name, title and message. Additionally, there is a submit button (named "submit").

表单位于/messages.php?action=send。要发送的消息需要填写三件事:名称为name,title和message的三个文本字段。此外,还有一个提交按钮(名为“提交”)。

How can I fill in this form and send it?

我如何填写此表并发送?

5 个解决方案

#1


11  

import urllib
import urllib2

name =  "name field"
data = {
        "name" : name 
       }

encoded_data = urllib.urlencode(data)
cOntent= urllib2.urlopen("http://www.abc.com/messages.php?action=send",
        encoded_data)
print content.readlines()

just replace http://www.abc.com/messages.php?action=send with the url where your form is being submitted

只需将http://www.abc.com/messages.php?action=send替换为提交表单的网址即可

reply to your comment: if the url is the url where your form is located, and you need to do this just for one website, look at the source code of the page and find

回复您的评论:如果网址是您的表单所在的网址,并且您只需要为一个网站执行此操作,请查看该网页的源代码并查找


and put this address as parameter for urllib2.urlopen

并将此地址作为urllib2.urlopen的参数

And you have to realise what submit button does. It just send a Http request to the url defined by action in the form. So what you do is to simulate this request with urllib2

你必须意识到提交按钮的作用。它只是向表单中的action定义的url发送一个Http请求。所以你要做的是用urllib2模拟这个请求

#2


7  

You want the mechanize library. This lets you easily automate the process of browsing websites and submitting forms/following links. The site I've linked to has quite good examples and documentation.

你想要机械化库。这使您可以轻松自动化浏览网站和提交表单/以下链接的过程。我链接的网站有很好的例子和文档。

#3


4  

You can use mechanize to work easily with this. This will ease your work of submitting the form. Don't forget to check with the parameters like name, title, message by seeing the source code of the html form.

您可以使用mechanize轻松地使用它。这将简化您提交表单的工作。不要忘记通过查看html表单的源代码来检查名称,标题,消息等参数。

import mechanize
br = mechanize.Browser()
br.open("http://mywebsite.com/messages.php?action=send")
br.select_form(nr=0)
br.form['name'] = 'Enter your Name'
br.form['title'] = 'Enter your Title'
br.form['message'] = 'Enter your message'
req = br.submit()

#4


1  

Try to work out the requests that are made (e.g. using the Chrome web developer tool or with Firefox/Firebug) and imitate the POST request containing the desired form data.

尝试计算所做的请求(例如,使用Chrome网站开发人员工具或使用Firefox / Firebug)并模仿包含所需表单数据的POST请求。

In addition to the great mechanize library mentioned by Andrew, in case I'd also suggest you use BeautifulSoup to parse the HTML.

除了Andrew提到的强大的机械化库之外,如果我还建议你使用BeautifulSoup来解析HTML。

If you don't want to use mechanize but still want an easy, clean solution to create HTTP requests, I recommend the excellend requests module.

如果您不想使用机械化但仍想要一个简单,干净的解决方案来创建HTTP请求,我建议使用excellend请求模块。

#5


1  

To post data to webpage, use cURL something like this,

要将数据发布到网页,请使用类似这样的cURL,

curl -d Name="Shrimant" -d title="Hello world" -d message="Hello, how are you" -d Form_Submit="Send" http://www.example.com/messages.php?action=send

curl -d Name =“Shrimant”-d title =“Hello world”-d message =“你好,你好吗”-d Form_Submit =“发送”http://www.example.com/messages.php?action=发送

The “-d” option tells cURL that the next item is some data to be sent to the server at http://www.example.com/messages.php?action=send

“-d”选项告诉cURL下一个项目是要发送到服务器的一些数据,网址为http://www.example.com/messages.php?action=send


推荐阅读
author-avatar
蓝沫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有