python中mechanize库的简单使用示例

yipeiwu_com5年前Python基础

复制代码 代码如下:

#!/usr/bin/env/ python
#coding=utf-8
import mechanize
import cookielib

# Cookie Jar
cj = cookielib.LWPCookieJar()

# Browser
br = mechanize.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(mechanize._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")

相关文章

Python实现加载及解析properties配置文件的方法

本文实例讲述了Python实现加载及解析properties配置文件的方法。分享给大家供大家参考,具体如下: 这里参考前面一篇:/post/137393.htm 我们都是在java里面遇...

python3 判断列表是一个空列表的方法

python3 判断空列表 @(python3) 有个判断列表是否为空的需求,试了好多方式,比如: a = [] if a is not None: COMMAND a =...

python如何为被装饰的函数保留元数据

本文实例为大家分享了python为被装饰的函数保留元数据的具体代码,供大家参考,具体内容如下 案例:        在函数对...

Pycharm更换python解释器的方法

安装了pycharm之后有一个新装的python解释器,顶替了之前系统的python 那样的话,原来利用pip安装的一些库会无法import. 要么加入环境变量,要么更换运行的解释器:...

详解python运行三种方式

方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。 linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口...