blackclody

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
1.课堂内容
requests 获取网页内容
rebots.txt 网络爬虫排除标准
BeautifulSoup  解析HTML页面
Re 正则表达式详解,提取页面关键信息
Scrapys 网络爬虫原理介绍专业爬虫框架介绍

2. pytho开发环境
文本工具类
IDLE Notepad++ sublime Text, vim,Atom,Komodo Edit
集成开发工具:
PyCharm, Wing, Pydev&Eclipse, Visual Studio,Anaconda&PTVS, Spyder,Canopy
推荐
pycharm(推荐)开发大型程序
Anaconda 科学计算数据分析领域的IDE,推荐,开源,免费
IDLE sublime

the website is API
Requests库
rebots协议
5个实战项目

1.Requests入门
requests:www.python-requests.org上获取requests库的详细信息
使用pip工具来安装requests库:pip install requests
requests的相关方法 
request.get(url,params =  None,**kwargs)构造一个Request对象 ,返回一个Response对象
url: 拟获取页面的url链接
params: urlk中的额外参数,字典或字节流格式,可选
**kwargs:12个控制访问的参数

Response对象的属性:
r.status_code HTTP请求返回的状态码,200表示连接成功,404表示失败
r.text  HTTP响应内容的字符串形式,即,url对应的页面内容
r.encoding 从HTTP头部猜测的响应内容编码方式,是从HTTP头部中charset字段获得的,如果header中不存在charset,则认为编码为ISO-8859-1,这种编码方式不能够解析中文
r.apparent_encoding 从内容中分析出的响应内容可能的编码方式,比encoding更加准确
r.content HTTP响应内容的二进制形式
  1. import requests
  2. r = requests.get('http://www.baidu.com')
  3. print r.status_code
  4. r.encoding = r.apparent_encoding
  5. print r.text
爬取网络的通用代码框架
Requsts库的异常
异常说明 
requests.Connectionerror网络连接错误异常,如DNS查询失败,拒绝连接等 
requests.HTTPErrorHTTP错误异常
requests.URLRequiredURL缺失异常
requests.TooManyRedirects超过最大的重定向次数,产生重定向异常
requests.ConnectTimeout连接远程服务器超时异常
requests.Timeout请求URL超时,产生超时异常
r.raise_for_status() 判断返回的status_code是不是200,若不是,抛出一个HTTPError异常
  1. #-*-coding = utf-8 -*-
  2. import requests
  3. def getHTMLText(url):
  4. try:
  5. r = requests.get(url,timeout=30)
  6. r.raise_for_status()
  7. r.encoding = r.apparent_encoding
  8. return r.text
  9. except Exception as e:
  10. print e
  11. if __name__ == "__main__":
  12. url = "http://www.baidu.com"
  13. print getHTMLText(url)
HTTP协议 超文本传输协议 
请求响应协议
host:主机名
port: 服务的端口,默认为80
path
对应于网络上的一个资源
HTTP协议对资源的操作
方法说明requests方法 
GET请求获取URL位置的资源requests.get(url,params=None,**kwargs)
HEAD请求获取URL位置的资源的响应消息的报告,即获得该资源的头部信息requests.head(url,**kwargs)
POST请求向URL位置的资源后附加新的数据requests.head(url,data=None,json=None,**kwargs)
PUT请求向URL位置存储一个资源,覆盖原URL位置的资源requests.put(url,data=None,**kwargs)
PATCH请求局部更新URL位置的资源,即改变该处资源的部分内容,节省网络带宽requests.patch(url,data=None,**kwargs)
DELETE请求删除URL位置存储的资源requests.delete(url,**kwargs)
requests.head()方法示例
  1. import requests
  2. r = requests.head("http://www.baidu.com")
  3. for item in r.headers.items():
  4. print "%s : %s"%item
输出
  1. Server : bfe/1.0.8.18
  2. Date : Tue, 23 May 2017 08:01:17 GMT
  3. Content-Type : text/html
  4. Last-Modified : Mon, 13 Jun 2016 02:50:47 GMT
  5. Connection : Keep-Alive
  6. Cache-Control : private, no-cache, no-store, proxy-revalidate, no-transform
  7. Pragma : no-cache
  8. Content-Encoding : gzip
requests.put()方法测试
  1. import requests
  2. payload = {'key1':'value1','key2':'value2'}
  3. r = requests.post("http://httpbin.org/put",data = payload)
  4. print r.text
输出
  1. {
  2. "args": {},
  3. "data": "",
  4. "files": {},
  5. "form": {
  6. "key1": "value1",
  7. "key2": "value2"
  8. },
  9. "headers": {
  10. "Accept": "*/*",
  11. "Accept-Encoding": "gzip, deflate",
  12. "Connection": "close",
  13. "Content-Length": "23",
  14. "Content-Type": "application/x-www-form-urlencoded",
  15. "Host": "httpbin.org",
  16. "User-Agent": "python-requests/2.13.0"
  17. },
  18. "json": null,
  19. "origin": "218.29.102.115",
  20. "url": "http://httpbin.org/put"
  21. }
requests.request(method,url,**kwargs)
method请求方式,可以是GET,HEAD,POST,PUT,PATCH,delete,OPTIONS
**kwargs 13个参数
params 字典或字节序列,作为参数增加到url中
示例
  1. >>> kv= {'key1':'value1','key2':'value2'}
  2. >>> r = requests.request("GET",'http://www.python123.io/ws',params=kv)
  3. >>> print r.url
  4. http://www.python123.io/ws?key2=value2&key1=value1
data: 字典,字节序列或文件对象,作为Request的内容
  1. >>> kv = {'key1':'value1','key2':'value2'}
  2. >>> r = requests.request("PUT",'http://www.python123.io/ws',data=kv)
  3. >>> body = '主体内容'
  4. >>> r = requests.request("PUT",'http://www.python123.io/ws',data=body)
json:JSON格式的数据,作为Request的内容
  1. >>> kv = {'key1':'value1','key2':'value2'}
  2. >>> r = requests.request("PUT",'http://www.python123.io/ws',json=kv)
headers:字典,HTTP定制头
cookies: 字典或CookieJar,Request中的cookie
auth:元组,支持HTTP的认证功能
files:字典类型,传输文件
  1. >>> fs = {'file':open('data.xls','rb'}
  2. >>> r = requests.request("POST",'http://www.python123.io/ws',files = fs)
timeout:设置超时时间,以秒为单位
proxies: 字典类型,设定访问代理服务器,可以增加登录认证,可以隐藏用户访问网页的源IP地址信息,能有效的防止对爬虫的逆追踪
示例
  1. >>> pxs ={'http':'http://user:pass@10.10.10.1:1234','https':'https://10.10.10.1.4321'}
  2. >>> r = requests.request('GET','http://www.baidu.com',proxies = pxs)
allow_redicts:True/False默认为True,重定向开头
stream:True/False,默认为True,获取内容立即下载开头
verify: True/False,默认为True,认证SSL证书开头
cert:本地SSL证书路径 


最常使用就是get和head方法
代码框架
  1. import requests
  2. def getHTMLText(url):
  3. try:
  4. r = requests.get(url,timeout = 30)
  5. r.raise_for_status()
  6. r.encoding = r.apparent_encoding
  7. return r.text
  8. except Exception as e:
  9. print e
实例1,京东商品页面的爬取
  1. #-*-coding=utf8-*-
  2. import requests
  3. url = "https://item.jd.com/1696975522.html"
  4. try:
  5. r = requests.get(url)
  6. r.raise_for_status()
  7. r.encoding = r.apparent_encoding
  8. print r.text[:1000]
  9. except Exception as e:
  10. print "爬取失败"
实例2:亚马逊商品页面爬取
  1. #-*-coding=utf8-*-
  2. import requests
  3. url = "https://www.amazon.cn/gp/product/B0186FESGW/ref=s9_acss_bw_cg_kin_1a1_w?pf_rd_m=A1U5RCOVU0NYF2&pf_rd_s=merchandised-search-2&pf_rd_r=MNPJWDEAVVCY67V3KKXB&pf_rd_t=101&pf_rd_p=190844af-fd7e-4d63-b831-fbd5601cfa0d&pf_rd_i=116087071"
  4. try:
  5. kv = {"user-agent":"Mozilla/5.0"}
  6. r = requests.get(url,headers = kv)
  7. r.raise_for_status()
  8. r.encoding = r.apparent_encoding
  9. print r.text[:1000]
  10. except Exception as e:
  11. print "爬取失败"
实例3:向搜索引擎提交关键字
百度的关键词接口
360的关键词接口
  1. #-*-coding=utf8-*-
  2. import requests
  3. #url = 'http://www.baidu.com/s'
  4. url = 'http://www.so.com/s'
  5. try:
  6. #kv = {'wd':'python'}
  7. kv = {'q':'python'}
  8. r = requests.get(url,params = kv)
  9. r.raise_for_status()
  10. print len(r.text)
  11. except Exception as e:
  12. print "爬取失败"
实例:爬取图片
  1. #-*-coding=utf8-*-
  2. import os
  3. import requests
  4. root = 'F:\\pythonstudy\\'
  5. url = 'http://image.nationalgeographic.com.cn/2017/0523/20170523015851567.jpg'
  6. path = root + url.split('/')[-1]
  7. if not os.path.exists(root):
  8. os.mkdir(root)
  9. if not os.path.exists(path):
  10. try:
  11. r = requests.get(url)
  12. r.raise_for_status()
  13. with open(path,'wb') as f:
  14. f.write(r.content)
  15. print u"写入成功"
  16. except Exception as e:
  17. print u"爬取失败"
  18. else:
  19. print u"文件已存在"
实例5:查询IP地址
  1. #-*-coding=utf8-*-
  2. import requests
  3. url = 'http://m.ip138.com/ip.asp?ip='
  4. try:
  5. r = requests.get(url+'222.222.222.222')
  6. r.raise_for_status()
  7. print r.text[-1000:]
  8. except Exception as e:
  9. print u"爬取失败"


网络爬虫的尺寸
小规模,数据量小的爬取速度不敏感 Requests库 爬取网页 玩转网页 >90%
中规模,数据规模较大爬取速度敏感 Scrapy库 爬取网站 爬取系列网站
大规模,搜索引擎爬取速度关键 定制开发  爬取全网

网络爬虫带来的问题:网络爬虫对网站的性能会有影响,具有一定的法律风险,导致个人隐私泄露

网络爬虫的限制:
         目标网站会首先判断 user-agent字段,只响应浏览器和友好爬虫的请求
        发布公告,robots协议 告诉所有爬虫网站的爬取策略,要求爬虫遵守
robots协议 Robots Exclusion Standard网络爬虫排除标准
网站告知爬虫那些页面可以抓取,那些不行
形式:在网站的根目录下的robots.txt文件
如:京东的robots协议 https://www.jd.com/robots
  1. User-agent: *
  2. Disallow: /?*
  3. Disallow: /pop/*.html
  4. Disallow: /pinpai/*.html?*
  5. User-agent: EtaoSpider
  6. Disallow: /
  7. User-agent: HuihuiSpider
  8. Disallow: /
  9. User-agent: GwdangSpider
  10. Disallow: /
  11. User-agent: WochachaSpider
  12. Disallow: /
robots协议的语法:
User-agent:*
Disallow: /
如果一个网站的根目录下没有robots文件,则网络爬虫可以无限制的爬取其上面的网络内容

robots协议的使用
网络爬虫:自动或人工识别robots.txt,再进行内容爬取
约束性:robots协议是建议但非线束性,网络爬虫可以不遵守,但存在法律风险





 













posted on 2017-05-23 21:07  blackclody  阅读(567)  评论(0编辑  收藏  举报