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高手之路python处理excel文件(方法汇总)

python高手之路python处理excel文件(方法汇总)

用python来自动生成excel数据文件。python处理excel文件主要是第三方模块库xlrd、xlwt、xluntils和pyExcelerator,除此之外,python处理e...

Python键盘输入转换为列表的实例

Python输入字符串转列表是为了方便后续处理,这种操作在考试的时候比较多见。 1.在Python3.0以后,键盘输入使用input函数 eg1. >>> x=in...

python opencv 批量改变图片的尺寸大小的方法

python opencv 批量改变图片的尺寸大小的方法

我目标文件夹下有一大批图片,我要把它转变为指定尺寸大小的图片,用pthon和opencv实现的。 以上为原图片。 import cv2 import os # 按指定图像大小调整尺...

python的dataframe转换为多维矩阵的方法

python的dataframe转换为多维矩阵的方法

最近有一个需求要把dataframe转换为多维矩阵,然后可以使用values来实现,下面记录一下代码,方便以后使用。 import pandas as pd import numpy...

Python和Sublime整合过程图示

Python和Sublime整合过程图示

这篇文章主要介绍了Python和Sublime整合过程图示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 按照下面的方式也可以运行py...