Python uses mechanize for Web page interaction

Source: Internet
Author: User

1. Stateful Web pages

A state-of-the-way Web page is a dynamic interactive page that differs from the traditional HTML, and it interacts with the user to display different results. Below I take the Sina mailbox landing interface as an example:
  
There is a user login form, when we enter the correct username password, login successfully to the Mailbox Operation window, otherwise give the error message.

2. Processing using the Mechanize library in Python

There are a few features to be aware of using the Mechanize library:
  

    • Mechanize automatically handles cookies.
    • To understand the process of interacting with a Web page.
    • Click on the link to fill out and submit the form
2.1 Landing Page

To use the Mechanize library in your code, you can install it using the following command if it is not installed.
  sudo pip install mechanize

I. 导入mechanize库,使用Browser()打开邮箱登陆页面。

>>>
>>> Import Mechanize
>>> br = mechanize. Browser ()
>>>
>>> br.open (' http://mail.sina.com.cn/')
< Response_seek_wrapper at 0XB6C1EB2CL whose wrapped object = < Closeable_response @ 0xb6c2164cl whose fp = < so Cket._fileobject Object at \0xb6c18b2c>>>
>>>

II. 列出网页中的表单。

>>>
>> for form in Br.forms ():
.. print Form
...
< POST http://mail.sina.cn/cgi-bin/sla.php?vt=3 application/x-www-form-urlencoded
< TextControl (local=) >
< Selectcontrol (domain=[*sina.com, sina.cn, 2008.sina.com, vip.sina.com]) >
< Passwordcontrol (pwd=) >
< Checkboxcontrol (remember=[*1]) >
< Submitcontrol (= login) (readonly) >>
>>>

III. 填写表单信息。

>>>
>>> Br.select_form (nr=0)
>>> br.form[' local '] = ' pythontesting '
>>> br.form[' pwd '] = ' test* #123 '
>>>

IV. 提交表单并查看返回代码

>>> Br.submit ()
< Response_seek_wrapper at 0XB6C1EB8CL whose wrapped object = < Closeable_response at 0x884ed8c whose fp = < sock Et._fileobject Object at 0x881fb6c>>>
>>>
>>> print Br.response (). Read ()

From the results, it has been successfully landed.

2.2 Click Link, jump page

I. First list the links contained in the mailbox page.
  

>>>
>> for link in br.links ():
... print Link.url + ': ' +link.text
...
/BASIC/?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3:[IMG]
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=new: Receive Letter
/basic/writemail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3: Write a letter
/basic/findmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3: Find
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=new: Collection clips (3)
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&type=1&fid=all: Unread messages (3)
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=cur: Draft clip
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=out: Sent
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=subs: Subscribe to Mail
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=TMP: Deleted
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=unk: Junk Mail
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=74: Website Notification
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=75: Order Billing
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=76: Social Networks
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=77: Subscription information
/BASIC/LISTMAIL.PHP?SID=4840C2EA0C566E95EA5B3BBE224125586&AMP;VT=3&AMP;FID=73: Business News
/basic/listmail.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3&fid=all: All messages (3)
/basic/logout.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3: Exit
/basic/about.php?sid=4840c2ea0c566e95ea5b3bbe224125586&vt=3: Help
>>>

II. Open the link to jump.

>>>
>>> New_link = Br.click_link (text= ' unread Mail (3) ')
>>>
>>> Br.open (New_link)
< Response_seek_wrapper at 0xb6c1e5ecl whose wrapped object = < Closeable_response at 0x8853a8c whose fp = < sock Et._fileobject Object at 0x881fdec>>>
>>>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python uses mechanize for Web page interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.