python 中使用 urllib2 伪造 http 报头的2个方法
方法1、
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#!/usr/bin/python# -*- coding: utf-8 -*-#encoding=utf-8#Filename:urllib2-header.py import urllib2import sys #抓取网页内容-发送报头-1send_headers = { 'Host':'www.jb51.net', 'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection':'keep-alive'} req = urllib2.Request(url,headers=send_headers)r = urllib2.urlopen(req) html = r.read() #返回网页内容receive_header = r.info() #返回的报头信息 # sys.getfilesystemencoding() html = html.decode('utf-8','replace').encode(sys.getfilesystemencoding()) #转码:避免输出出现乱码 print receive_header# print '####################################'print html |
方法2、
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/usr/bin/python# -*- coding: utf-8 -*-#encoding=utf-8#Filename:urllib2-header.py import urllib2import sys req = urllib2.Request(url)req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0')r = urllib2.urlopen(req) html = r.read()receive_header = r.info() html = html.decode('utf-8').encode(sys.getfilesystemencoding()) print receive_headerprint '#####################################'print html |
python 中使用 urllib2 伪造 http 报头的2个方法的更多相关文章
- python中使用urllib2伪造HTTP报头的2个方法
在采集网页信息的时候,经常需要伪造报头来实现采集脚本的有效执行 下面,我们将使用urllib2的header部分伪造报头来实现采集信息 方法1. ? 1 2 3 4 5 6 7 8 9 10 11 1 ...
- python中urllib, urllib2,urllib3, httplib,httplib2, request的区别
permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...
- 在python中创建列表的最佳和/或最快方法
在python中,据我所知,至少有3到4种方法来创建和初始化给定大小的列表: 简单循环append: my_list = [] for i in range(50): my_list.append(0 ...
- 为什么Python中称__lt__、__gt__等为“富比较”方法
Python中基类object提供了一系列可以用于实现同类对象进行"比较"的方法,可以用于同类对象的不同实例进行比较,包括__lt__.__gt__.__le__.__ge__._ ...
- 第14.12节 Python中使用BeautifulSoup解析http报文:使用select方法快速定位内容
一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>和<第14.11节 Python中使用BeautifulSo ...
- python中 urllib, urllib2, httplib, httplib2 几个库的区别
转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...
- Python中的urllib2模块解析
Name urllib2 - An extensible library for opening URLs using a variety of protocols 1. Description Th ...
- python中List添加、删除元素的几种方法
一.python中List添加元素的几种方法 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持着初始时的定义的顺序(除非你对它们进行排序或其他修改操作).在Pytho ...
- Python中各种括号的区别、用途及使用方法
python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也叫做花括号{ }.其作用也各不相同,分别用来代表不同的python基本内置数据类型. python中的小括号( ):代表 ...
随机推荐
- [iOS]UIScrollView左右拨动,第二页宽度只有一半问题
用UIScrollView动态加入新View,而这个View是Xib方式创建,如果设置view的frame,这个view的宽度却只有设置的一半,很奇怪.于是我只设置view的frame的x值,不设置整 ...
- failed to open stream: No such file or directory 报错解决方法
1.首先检查是否是文件名错误(比如有空格):是否因为路径不完整(比如缺少http://,或者缺少文件扩展名.doc等): 2.若是在本地中文名文件打开报错,我就是因为编码不一致导致: Windows中 ...
- 在Windows10系统中配置和运行MongoDB数据库,linux开启mongdb
参考链接:http://jingyan.baidu.com/article/11c17a2c03081ef446e39d02.html linux中开启mongodb服务: 1. 进入到/data/ ...
- Culling & Depth Testing
[Culling & Depth Testing] Culling is an optimization that does not render polygons facing away f ...
- 编码总结,以及对BOM的理解
一.前言 在跨平台.跨操作系统或者跨区域之间,经常会涉及到编码的问题,因为前段时间在项目中,遇到了因为编码而产生乱码的问题,以前对编码也是一知半解,所以决定对编码有一个更为深入的了解,因此才有了这篇自 ...
- 49. Group Anagrams同义词合并
[抄题]: Given an array of strings, group anagrams together. Example: Input: ["eat", "te ...
- Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'
pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...
- Can not find -lXXXX
Description I had the error message Can not find -lGL when i run qt qmake long ago. The error messag ...
- C程序之包含头文件
在C程序中包含文件有以下两种方法: 方法一:#include<XXX.h> 这里的XXX一般是改动较小的标准库,用符号"<"和">"将要 ...
- spring.net事件的注入
.c#代码 TestObject source = new TestObject(); TestEventHandler eventListener1 = new TestEventHandler() ...