Log on to the website using Python

Source: Internet
Author: User
Log on to the website using Python

For most forums, we need to log in first to capture the posts for analysis. Otherwise, we cannot view them.

This is because the HTTP protocol is a stateless (stateless) protocol. How does the server know whether the user requesting the connection has logged on? There are two methods:

  1. Explicitly use the session ID in the URI;
  2. The process of using cookies is that a cookie will be retained locally after you log on to a website. When you continue to browse the website, the browser will send the cookie along with the address request.

Python provides quite a variety of modules, so you can complete this network operation in just a few words. I log on to the qzzn Forum as an example. In fact, almost all phpwind forums of the following program are applicable.

#-*-Coding: gb2312 -*-

From urllib import urlencode
Import cookielib, urllib2

# Cookie
Cj = cookielib. lwpcookiejar ()
Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))
Urllib2.install _ opener (opener)

# Login
User_data = {'pwuser': 'Your username ',
'Pwpwd': 'Your password ',
'Step': '2'
}
Url_data = urlencode (user_data)
Login_r = opener. Open ("http://bbs.qzzn.com/login.php", url_data)

Some notes:

  1. Urllib2 is obviously more advanced than urllib, which includes how to use cookies.
  2. In urllib2, each client can use an opener for abstraction, and each opener can add multiple handler to enhance its functions.
  3. Httpcookieprocessor is specified as handler when constructing opener. Therefore, this handler supports cookie.
  4. After isntall_opener is used, this opener is used when urlopen is called.
  5. If you do not need to save the cookie, the CJ parameter can be omitted.
  6. User_data stores the information required for login. When logging on to the Forum, you can pass this information over.
  7. The urlencode function encodes the dictionary user_data "? Pwuser = username & pwpwd = password "to make the program easier to read.

The last problem is where names such as pwuser and pwpwd come from, so we need to analyze the web pages to be logged on. We know that the general logon interface is a form. The excerpt is as follows:

<Form action = "login. php? "Method =" Post "name =" login "onsubmit =" This. Submit. Disabled = true; ">

<Input type = "hidden" value = "" name = "Forward"/>
<Input type = "hidden" value = "http://bbs.qzzn.com/index.php" name = "jumpurl"/>

<Input type = "hidden" value = "2" name = "Step"/>
...
<TD width = "20%" onclick = "document. login. pwuser. focus (); "> <input type =" radio "name =" LGT "value =" 0 "checked/> User Name <input type =" radio "name =" LGT "value =" 1" /> uid </TD>

<TD> <input class = "input" type = "text" maxlength = "20" name = "pwuser" size = "40" tabindex = "1"/> <a href = "reg1ster. PHP "> Register now </a> </TD>

<TD> password </TD>
<TD> <input class = "input" type = "password" maxlength = "20" name = "pwpwd" size = "40" tabindex = "2"/> <a href = "sendpwd. PHP "target =" _ blank "> retrieve password </a> </TD>

...
</Form>

From this we can see that the user name and password we need to enter correspond to pwuser and PWD, while the step corresponds to login (this is an attempt ).

Note that this forum form uses the POST method. If it is the get method, the method in this article needs to be changed. Instead of open directly, you should first request and then open. For more details, see the manual...

Related Article

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.