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 ")