的Python:点击一个按钮 [英] Python: clicking a button

查看:771
本文介绍了的Python:点击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在点击这个按钮看起来在HTML code这样的问题:

I have problems in clicking this button that looks in HTML code like this:

<form method="post">
<br>
<input type="hidden" value="6" name="deletetree">
<input type="submit" value="Delete Tree" name="pushed">
</form>

和需要生成看起来像这样的网址:
<一href=\"http://mysite.com/management.php?Category=2&id_user=19&deteletree=6&pushed=Delete+Tree\" rel=\"nofollow\">http://mysite.com/management.php?Category=2&id_user=19&deteletree=6&pushed=Delete+Tree

and the url that needs to be generated looks like this: http://mysite.com/management.php?Category=2&id_user=19&deteletree=6&pushed=Delete+Tree

更新:
我想这一点,但它不工作:

Update: I tried this, but it doesnt work:

form_data = urllib.urlencode({'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree' })
urllib2.urlopen("management.php", form_data)

这是我的登录方式:

cj = cookielib.CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13')] 
username = "user" 
password = "pass" 
USER_ID = '6'

    loginonsite = login("http://mysite.com/myprofile.php",
                        "login_username=%s&login_password=%s&suid=%s".format(username, password, USER_ID)

推荐答案

您可以使用请求,使一个职位。

You could use requests to make a post.

import requests
data = {'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree' }
response = requests.post('http://mysite.com/management.php', data=data)

print response.text

随着越来越多的网页的内容在JavaScript中产生,我觉得使用Selenium的自己的webdriver直接驱动,例如Chrome真正的浏览器,当我现在做这种自动化...

As more and more of the content of a webpage is generated in JavaScript I find myself using Selenium's webdriver to directly drive a real browser like Chrome when I'm doing this kind of automation now...

更新:听起来你需要先登录

现在,请求可以通过,以及通过饼干。所以,你要发送登录请求,你可以这样做

Now, requests can pass cookies through as well. So you to send a logged in request you would do this

login_data = data={'username': 'user', 'password': 'pass'
post_data = {
    'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree'
}
login_response = requests.get('http://mysite.com/myprofile.php', data=login_data)
form_response = requests.post(
    'http://mysite.com/management.php',
     data=post_data, 
     cookies=login_response.cookies
)

所以,你做的登录,然后用饼干在接下来的请求的响应。应该工作。但很明显我不能测试code为您确切的情况。

So, you do the login, then use the cookies in the response in the next request. Should work. But obviously I can't test that code for your exact situation.

这篇关于的Python:点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆