python爬虫之一——urllib、urllib2篇

本文介绍使用Python进行网页爬取的基本方法,包括GET和POST请求、处理HTTP代理及调试技巧等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面:py的和其他语言相比,精华就在库非常多,优雅,简洁的引用;短短几句像诗句一样,包含着无限韵味,完全没必要死记硬背,用到时候看几眼,大体都会用了;
环境:win7、32位;py:2.7
文档参考地址:urllib英文文档

一、爬站分析:

我们的目标是爬取网站上的讯息,既然要爬取,那就要了解反爬;就和拳击选手一样,要打倒对手,首先要学会挨打;我们分析一下目前网络上大部分流行的网站我们,无非三种语言:php、asp、java最多;我们要爬取一个页面,自然要对这其中的一种有所了解:就选取世界上最好的语言php吧:

1.登录验证 :主要是cookie存储、读取和验证码的处理,很有挑战性;
2.cookie拦截:是通过为页面设置cookie,如果验证通过,发送资源,不通过,显示403;
3.User-Agent检测:为了判断是否为爬虫,如果headers中缺少User-Agent,那么判定为爬虫。通过这个系统函数可以判断

  <?php
    $_SERVER['HTTP_USER_AGENT']
  ?>

4.ip访问次数过多或者频繁;这点估计也只有大厂和技术密集的公司会用到了;如果是类似restful资源型的,肯定是会检测的,比如php的yii2.0框架就有对资源访问频率做出限制。
5.其他限制:包括ajax的完全加密、滑动式验证码、点击式验证码…

二、资源的爬取、读入
  1. get和post 参数的encode:urllib.encode(query,doseq=0)
  2. 请求资源:urllib2.Request(url,data,headers)
  3. 打开资源: resource = urllib2.urlopen(url或者定制的request,[data,timeout])
  4. 读入资源:resource.read()

以上搭配正则就可以操作简单的爬虫了;

1>source = urllib2.open(url|request,[,data,timeout])

source得到对象:
方法有:
- source.getcode() :获取响应码
- source.geturl() :判断是否重定向
- source.info() :获取响应的headers

2>urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])

Request是个对象
方法有:
- Request.add_data(data)
- Request.get_method()
- Request.has_data()
- Request.get_data()
- Request.add_header(key, val)
- Request.has_header(header)
- Request.get_full_url()
- Request.get_type()
- Request.get_header(header_name, default=None)
……
可去参考手册去查看:

三、资源请求的构造

1>get方式(追加构造url):

# -*- coding:utf-8 -*-
import urllib
import urllib2
data = {'country':'China','age':32}
formatdata = urllib.urlencode(data)
url  = "http://cuiqingcai.com/947.html"+'?'+formatdata
response = urllib2.urlopen(url)
print response.geturl()

输出:
http://cuiqingcai.com/947.html?country=China&age=32

2>post方式(填充data):

# -*- coding:utf-8 -*-
import urllib
import urllib2
data = {'country':'China','age':32}
formatdata = urllib.urlencode(data)
url  = "http://cuiqingcai.com/947.html"
response = urllib2.urlopen(url, formatdata)
print response.info()

输出结果:
Server: nginx/1.10.1
Date: Tue, 20 Jun 2017 03:48:03 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Vary: Cookie
X-Pingback: http://cuiqingcai.com/xmlrpc.php
Link: <http://cuiqingcai.com/wp-json/>; rel="https://api.w.org/"
Link: <http://cuiqingcai.com/?p=947>; rel=shortlink

3>其他方式:

# -*- coding:utf-8 -*-
import urllib2
request = urllib2.Request(url, data=data)
request.get_method = lambda: 'PUT'|'DELETE'
response = urllib2.urlopen(request)
四、debug设置
# -*- coding:utf-8 -*-
import urllib
import urllib2
httphander  = urllib2.HTTPHandler(debuglevel=1)
httpshander = urllib2.HTTPSHandler(debuglevel=1)
build_opener= urllib2.build_opener(httphander,httpshander)
urllib2.install_opener(build_opener)
urllib2.urlopen('http://www.news.com/')
五、设置http代理
# -*- coding:utf-8 -*-
import urllib2
proxy_handler = urllib2.ProxyHandler({'http': '....'})
opener = urllib2.build_opener(proxy_handler)
resouorce = opener.open('url')
print(resource.read())
#配置到全局
urllib2.install_opener(opener)
resouorce = opener.open('url')
print(resource.read())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值