Python预备知识

Requests库入门

Requests.requests()

requests.get()

requests.head()

requests.post()

requests.put()

requests.patch()

requests.delete()

作为爬虫工具,以上的get()和head()方法是最常用的

一定记住这个抛出异常代码块:

try:
    r=requests.get(url,timeout=
30)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
   
return r.text
except:
   
return "产生异常"

 

  1. 网络爬虫的尺寸:
  1. 爬取网页,玩转网页(小规模,数据量小,爬取速度不敏感)。使用工具:Requests 库 (占比:>90%)
  2. 爬取网站,爬起系列网站(中规模,数据规模较大,对速度敏感)。使用工具:Scrapy库。
  3. 爬取全网(全Internet)(大规模,搜索引擎爬取速度关键)。使用工具:是定制开发的工具。
  1. 限制网络爬虫
  1. 来源审查:判断User-Agent进行限制;检查来访HTTP协议头的User-Agent域,只响应浏览器或友好爬虫的访问。
  2. 发布公告:(通过robots协议发布公告
  1. robots协议的使用

user_agent

Disallow

网络爬虫盗亦有道:理论上,都应该遵守robots协议,不然会存在法律风险

但是当我们编写爬虫的访问模式与人为访问网页类似,并不作为商业用途时,原则上不用遵守robots协议。

  1. 爬取网页的代码
  2. import requests url = 'http://growthbox.net/growthhack/category/growthday/' try:     r = requests.get(url)     r.raise_for_status()     r.encoding = r.apparent_encoding     print(r.text[:1000]) except:     print('爬取失败')
 
  1. 向百度或360提供关键词并返回搜索结果

 

  1. 爬取网络图片,存储图片(用到文件的读写操作)

 

  1. Ip地址归属地的查询(判断该ip地址来源于北京,上海,还是美国呢)

 

二、解析HTML页面信息标记与提取方法

使用Beautiful  soup

 

 

猜你喜欢

转载自blog.csdn.net/kakufeier/article/details/84965331