热门标签 | 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


推荐阅读
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 本文介绍了Sencha Touch的学习使用心得,主要包括搭建项目框架的过程。作者强调了使用MVC模式的重要性,并提供了一个干净的引用示例。文章还介绍了Index.html页面的作用,以及如何通过链接样式表来改变全局风格。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
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社区 版权所有