Simple example of using the mechanism library in python

Source: Internet
Author: User

Copy codeThe Code is as follows:
#! /Usr/bin/env/python
# Coding = UTF-8
Import mechanic
Import cookielib

# Cookie Jar
Cj = cookielib. LWPCookieJar ()

# Browser
Br = mechanic. Browser ()
Br. set_cookiejar (cj)

# Browser options
Br. set_handle_equiv (True)
Br. set_handle_gzip (True)
Br. set_handle_redirect (True)
Br. set_handle_referer (True)
Br. set_handle_robots (False)

# Follows refresh 0 but not hangs on refresh> 0
Br. set_handle_refresh (mechanic. _ http. HTTPRefreshProcessor (), max_time = 1)

# Want debugging messages?
# Br. set_debug_http (True)
# Br. set_debug_redirects (True)
# Br. set_debug_responses (True)

# User-Agent (http header)
Br. addheaders = [('user-agent', 'mozilla/5.0 (X11; U; Linux i686; en-US; rv: 1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1. fc9 Firefox/4.0.0 ')]

# HTTP access and get response pack
Br. open ("http://hi.baidu.com/alalmn ")
Print br. response (). read (). decode ("UTF-8 ")

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.