用python自动查询网页并填写表格_使用Python登录网站,填写表格,然后退出

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.

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").

How can I fill in this form and send it?

解决方案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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值