Python Third party Library series 24--http-web Library __web

Source: Internet
Author: User
Tags http authentication urlencode

A total of 6 kinds of library recommended, strongly recommend requests library.

One of the Web libraries: Httplib Library

#!/usr/bin/env python
#coding =utf8

import httplib

httpclient = None

try:
    httpclient = Httplib. Httpconnection (' www.baidu.com ', timeout=30)
    httpclient.request (' Get ', '/')

    #response是HTTPResponse对象
    response = Httpclient.getresponse ()
    print response.status
    print Response.reason
    print Response.read ()
except Exception, E:
    print e
finally:
    if httpclient:
        httpclient.close ()

Web Library bis: Urllib Library

The HTTP implementation is used in the Python Higher-level Encapsulation module (URLLIB,URLLIB2).

# Coding=utf8
Import urllib

baidu = Urllib.urlopen (' http://www.baidu.com ')
print baidu.read ()
print ' http header:/n ', Baidu.info ()
print ' HTTP status: ', Baidu.getcode ()
print ' URL: ', Baidu.geturl () For line
in Baidu: # Just like in the operation local file
    print line,
baidu.close ()

Web Library III: URLLIB2 Library--basic and Digest authentication, redirections, cookies and more.

#coding: utf-8
import urllib 
import urllib2  
 
url = ' http://www.baidu.com/s '  
values = {' WD ': ' Yang Yanxing '}   
data = Urllib.urlencode (values)
print data 
url2 = url+ '? ' +data
response = Urllib2.urlopen (url2)  
the_page = Response.read ()

print The_page
Web Library four: Cookielib Library
#coding: utf-8
import urllib2,urllib
import cookielib

url = R ' http://www.renren.com/ajaxLogin '

#创建一个cj的cookie的容器
CJ = Cookielib. Cookiejar ()
opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ)
#将要POST出去的数据进行编码
data = Urllib.urlencode ({"Email": Email, "Password":p ass})
r = Opener.open (url,data)
print CJ

When you see CJ, you have access to the login page, whether the normal login you can not see it now, access to http://www.renren.com/home to view

The above code has two points to explain, I also watched for a long time to understand

R = Opener.open (url,data), why use opener object to open instead of utllib2,urlopen? It's not just in the example that we write, Through the transformation we can also use urllib2.urlopen, in fact, because opener is created by Urllib2.bulid_opener, but you can understand that, he built out, but did not install the use of it, there is no its properties and methods, If you want to make URLLIB2 also have opener properties and methods, you can use Urllib2.install_opener (opener) to "install" This opener, after installation can use URLLIB2 to operate

Web Library five: Httplib2 Library

1.http.client is the implementation of RFC 2616, HTTP protocol of the underlying library;

2.urllib.request is built on an abstraction layer above http.client. It provides a standard API for accessing HTTP and FTP servers, can automatically follow HTTP redirection, and handles some common forms of HTTP authentication;

3.HTTPLIB2 is a third-party open source library that implements the HTTP protocol more fully than Http.client, while providing a better abstraction than urllib.request.

#coding =utf8

Import httplib2

#获取HTTP对象

H =httplib2. Http ()

#发出同步请求, and get content

resp, contents = h.request ("http://www.weirdbird.net/")
Print resp
print Content

HTTPLIB2 includes these features: HTTPLIB2 cache processing, HTTPLIB2 processing last-modified and ETag headers, http2lib processing compression, HTTPLIB2 processing redirection, and so on.

Web Library VI: Requests Library

Official website: http://www.python-requests.org/en/latest/



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.