Urllib Learning notes in Python

Source: Internet
Author: User
Tags join urlencode in python

Just learned about Python recently, Python's Urllib module may be faster and simpler than PHP's curl. Put a little bit of code

The code is as follows Copy Code
#_ *_ Coding:utf-8 _*_
Import Sys
Reload (SYS)
Sys.setdefaultencoding ("Utf-8")
Import OS
Import JSON
From Urllib import Urlopen
doc = Urlopen ("http://xxxx?webid=1&tid=901&cateid=101"). Read ()
doc = Json.loads (doc)
Print doc
Print Doc.keys ()
Print doc["MSG"]
Print doc[' data ']
Print doc[' ret ']

The time required to discover the first visit is [finished in 3.0s]

And the time for the second visit is [finished in 0.2s]

The Python urllib module is visible with a cache

Typical examples of URLLIB/2 usage

The code is as follows Copy Code

Import Urllib2
Import Cookielib
Import Urllib

Class Hi_login:
def __init__ (self):
Cookie = Cookielib. Cookiejar ()
Self.cookie = Urllib2. Httpcookieprocessor (cookie) ##### Generate Cookies ###

def login (self,user,pwd):
Url= ' Http://passport.baidu.com/?login '
Postdata=urllib.urlencode ({

' Mem_pass ': ' On ',

' Password ':p WD
' Submit ': ',
' TPL ': ' SP ',
' Tp_reg ': ' SP ',
' U ': ' http://hi.baidu.com ',
' username ': User})
### Proxy_support = urllib2. Proxyhandler ({"http": "http://ahad-haam:3128"}) and then join the opener method
Opener = Urllib2.build_opener (Self.cookie) ### using Cookies ###
headers = {####### dict structure, can join x-forward-for or even refer, etc. #######
' User-agent ': ' mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv: gecko/20091201 firefox/3.5.6 '}

Urllib2.install_opener (opener)
Request = Urllib2. Request (Url,urllib.urlencode (postdata), headers = headers)
Urllib2.urlopen (Request)

If __name__== ' __main__ ':
Pwd= ' 123456 '
User= ' Xiaofu '
Test=hi_login ()
Test.login (USER,PWD)

If you visit a page that requires authentication, such as a Nagios monitor page,

The code is as follows Copy Code

Import Urllib2
Password_mgr = Urllib2. Httppasswordmgrwithdefaultrealm ()

url = "Http://202.1.x.y/nagios"

Password_mgr.add_password (None, URL, user= ' abc ', passwd= ' xxxxxx ')

Handler = Urllib2. Httpbasicauthhandler (Password_mgr)

Opener = Urllib2.build_opener (handler)

Urllib2.install_opener (opener)

F=urllib2.urlopen (URL)
Print F.code

Return result 200, or 401 authentication error

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.