python中mechanize库的简单使用示例

yipeiwu_com6年前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利用datetime模块计算时间差

今天写了点东西,要计算时间差,我记得去年写过,于是今天再次mark一下,以免自己忘记 In [27]: from datetime import datetime In [28]:...

基于django传递数据到后端的例子

最近遇到一个问题,前端表单我写了多个按钮,每个按钮通过for循环来给name赋值如下: <input type="button" class="btn btn-info btn...

可用于监控 mysql Master Slave 状态的python代码

复制代码 代码如下:import osimport sysimport MySQLdbdef getStatus(conn):    query = " S...

django-初始配置(纯手写)详解

我们通过django-admin startproject zhuyu命令创建好项目后,在pycharm中打开 我们需要在在该项目中,配置一些相关操作。 1、template(存放模板的...

python实现决策树分类

python实现决策树分类

上一篇博客主要介绍了决策树的原理,这篇主要介绍他的实现,代码环境python 3.4,实现的是ID3算法,首先为了后面matplotlib的绘图方便,我把原来的中文数据集变成了英文。 原...