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

下面,我们将使用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. 洛谷 U4704 函数

    设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b)=?这样的题目,但是kkk全部理解成了xor( ...

  2. 安卓EditText按钮

    main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  3. Python大神成长之路: 第一次学习记录

    一.Python发展史 二.Python2 or 3 博主选择了Python3. 从官网下载Python  www.python.org Windows安装python3.5.python2.7.安装 ...

  4. WebDriver(Selenium2) 判断页面是否刷新的方法

    http://uniquepig.iteye.com/blog/1568208 public static boolean waitPageRefresh(WebElement trigger) { ...

  5. spring和springMVC的上下文

    上下文可以替代注解, 但是注解更方便 package com.tgb.web.controller; import javax.annotation.Resource; import javax.se ...

  6. ADO。net学习笔记

    来源于网络 1.       SqlConnection(DBConnection)  建立程序与数据库的链接 链接字符串有两种形式: //使用Windows验证  SSPI(安全支持提供程序接口) ...

  7. 用c++写一个 “hello,world” 的 FastCGI程序

    原文:http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/ 上面的连接地址给出的是作者的原文地址. 另外一个作者稍微 ...

  8. ural1126 Magnetic Storms

    Magnetic Storms Time limit: 0.5 secondMemory limit: 64 MB The directory of our kindergarten decided ...

  9. PAT (Advanced Level) 1003. Emergency (25)

    最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...

  10. [iOS]C语言技术视频-05-程序循环结构(do{}while();)

    下载地址: 链接: http://pan.baidu.com/s/1c02ke3m 密码: x97w