Python login website details and examples, python login website details

Source: Internet
Author: User

Python login website details and examples, python login website details

Python logon website details and examples

For most forums, we need to log on 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. When using cookies, a Cookie is stored locally after you log on to a website. When you continue to browse the website, the browser sends 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 urlencodeimport cookielib, urllib2 # cookiecj = cookielib. LWPCookieJar () opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cj) urllib2.install _ opener (opener) # Loginuser_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:

  • Urllib2 is obviously more advanced than urllib, which includes how to use Cookies.
  • In urllib2, each client can use an opener for abstraction, and each opener can add multiple handler to enhance its functions.
  • HTTPCookieProcessor is specified as handler when constructing opener. Therefore, this handler supports Cookie.
  • After isntall_opener is used, this opener is used when urlopen is called.
  • If you do not need to save the Cookie, the cj parameter can be omitted.
  • User_data stores the information required for Logon. you can pass this information when you log on to the Forum.
  • 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 webpage to be logged on. We know that the Common 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> <input class = "input" type = "text" maxLength = "20" name = "pwuser" size = "40" tabindex =" 1 "/> <a href =" reg1ster. php "rel =" external nofollow "> Register now </a> </td> <td> password </td> <input class =" input "type = "password" maxLength = "20" name = "pwpwd" size = "40" tabindex = "2"/> <a href = "sendpwd. php "rel =" external nofollow "target =" _ blank "> retrieve the 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 logon (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...

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.