在采集网页信息的时候,经常需要伪造报头来实现采集脚本的有效执行

下面,我们将使用urllib2的header部分伪造报头来实现采集信息

方法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 urllib2
import sys
  
#抓取网页内容-发送报头-1
send_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 urllib2
import sys
  
  
req = urllib2.Request(url)
req.add_header('Referer','http://www.jb51.net/')
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_header
print '#####################################'
print html

python中使用urllib2伪造HTTP报头的2个方法的更多相关文章

  1. 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/pyth ...

  2. python中urllib, urllib2,urllib3, httplib,httplib2, request的区别

    permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...

  3. 在python中创建列表的最佳和/或最快方法

    在python中,据我所知,至少有3到4种方法来创建和初始化给定大小的列表: 简单循环append: my_list = [] for i in range(50): my_list.append(0 ...

  4. 为什么Python中称__lt__、__gt__等为“富比较”方法

    Python中基类object提供了一系列可以用于实现同类对象进行"比较"的方法,可以用于同类对象的不同实例进行比较,包括__lt__.__gt__.__le__.__ge__._ ...

  5. 第14.12节 Python中使用BeautifulSoup解析http报文:使用select方法快速定位内容

    一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>和<第14.11节 Python中使用BeautifulSo ...

  6. python中 urllib, urllib2, httplib, httplib2 几个库的区别

    转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...

  7. Python中的urllib2模块解析

    Name urllib2 - An extensible library for opening URLs using a variety of protocols 1. Description Th ...

  8. python中List添加、删除元素的几种方法

    一.python中List添加元素的几种方法 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持着初始时的定义的顺序(除非你对它们进行排序或其他修改操作).在Pytho ...

  9. Python中各种括号的区别、用途及使用方法

    python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也叫做花括号{ }.其作用也各不相同,分别用来代表不同的python基本内置数据类型. python中的小括号( ):代表 ...

随机推荐

  1. 转 spring security的使用

    [转自:http://haohaoxuexi.iteye.com/blog/2154714] 关于登录 目录 1.1     form-login元素介绍 1.1.1    使用自定义登录页面 1.1 ...

  2. strut1.X和spring整合的二种方法

    第一种集成方法 原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象 缺点:产生了依赖,spring的类在action中产生了依赖查找.(注意和依赖注入 ...

  3. 文件夹添加 IIS 应用程序池用户权限

    http://serverfault.com/questions/81165/how-to-assign-permissions-to-applicationpoolidentity-account ...

  4. UILabel 解析及自适应

    CGFloat width1=[(NSString *)ob1 sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMa ...

  5. Byte数组和Int的互相转换

    public static int bytesToInt(byte[] bytes) { int addr = bytes[0] & 0xFF; addr |= ((bytes[1] < ...

  6. [转]异常:android.os.NetworkOnMainThreadException

    Android 4.1项目:使用新浪微博分享时报: android.os.NetworkOnMainThreadException 网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请 ...

  7. Android------>TableLayout表格布局方式

    main.xml表格代码分析 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmln ...

  8. 天梯赛决赛 L2-1.红色警报 并查集

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  9. ural1414 Astronomical Database

    Astronomical Database Time limit: 2.0 secondMemory limit: 64 MB After the Hubble telescope had been ...

  10. Max Flow

    Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...