开发者

How to click check boxes, put text in text boxes and click buttons using Python 3.x (which does not have the mechanize library)?

I am a newbie t开发者_JAVA技巧o Python. Started on Python 2.7 but quickly jumped to 3.2. I require a Python script that logs in to a password secure website (already done) and then do some operations such as writing to text box, clicking buttons etc. Life would have been easier with 'mechanize' I suppose (but it is not there for 3.x). Is there any other way? Please help.


Instead of finding the text box and writing in it, then finding the submit button and clicking in it, you can just submit the finished form instead. There are various tools for browsers like Chrome and Firefox that let you see the fields of the form so you know what you submitted. Then you can replicate that in Python with urllib:

>>> import urllib.request
>>> import urllib.parse
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> params = params.encode('utf-8')
>>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
>>> print(f.read().decode('utf-8'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜