网络采集系列文章
Python 网络数据采集之创建爬虫
Python 网络数据采集之 html 解析
开始采集
网络爬虫是可以根据递归方式为了找到 URL 链接, 它们必须首先获取网页内容, 检查这个页面的内容, 再寻找另一个 URL, 然后获取 URL 对应的网页内容, 不断循环这一过程
遍历单个域名
以抓取维基百科的 Kevin Baco 为例, 实现简单的数据爬取代码如下:
- from urllib.request import urlopen
- from bs4 import BeautifulSoup
- html = urlopen('http://en.wikipedia.org/wiki/Kevin_Bacon')
- bs = BeautifulSoup(html, 'html.parser')
- for link in bs.find_all('a'):
- if 'href' in link.attrs:
- print(link.attrs['href'])
上述代码就获取维基百科网站的任何页面并提取页面链接去掉我们不需要的词条, 进行改进获取词条链接代码如下:
- from urllib.request import urlopen
- from bs4 import BeautifulSoup
- import re
- html = urlopen('http://en.wikipedia.org/wiki/Kevin_Bacon')
- bs = BeautifulSoup(html, 'html.parser')
- for link in bs.find('div', {'id':'bodyContent'}).find_all(
- 'a', href=re.compile('^(/wiki/)((?!:).)*$')):
- if 'href' in link.attrs:
- print(link.attrs['href'])
如果我们要做到一个函数 getLinks, 可以用维基百科词条
/wiki/< 词条名称 >
形式的 URL 链接作为参数, 然后以同样的形式返回一个列表, 里面包含所有的词条 URL 链接一个主函数, 以某个起始词条为参数调用 getLinks, 再从返回的 URL 列表里随机选择一个词条链接, 再调用 getLinks, 直到我们主动停止, 或者在新的页面上没有词条链接了, 程序才停止运行
完善后代码如下:
- from urllib.request import urlopen
- from bs4 import BeautifulSoup
- import datetime
- import random
- import re
- random.seed(datetime.datetime.now())
- def getLinks(articleUrl):
- html = urlopen('http://en.wikipedia.org{}'.format(articleUrl))
- bs = BeautifulSoup(html, 'html.parser')
- return bs.find('div', {'id':'bodyContent'}).find_all('a', href=re.compile('^(/wiki/)((?!:).)*$'))
- links = getLinks('/wiki/Kevin_Bacon')
- while len(links)> 0:
- newArticle = links[random.randint(0, len(links)-1)].attrs['href']
- print(newArticle)
- links = getLinks(newArticle)
遍历整个网络
采集整个网站, 那是非常耗费内存资源的过程, 尤其是处理大型网站时, 最合适的工具就是用一个数据库来储存采集的资源
遍历整个网络的数据好处: 可以生产网站地图, 收集网站数据等但是一般不会消耗大量的内存资源去做采集, 本节的意义不大
通过互联网采集
建立网络地图, 需要从一个链接跳转到另一个链接, 逐步建立网站的地图信息, 从而达到一个合适的网站地图, 于此同时我们也不能忽略外链, 从一个链接跳转到另一个链接的场景也会时有发生相比之前的单域名采集, 互联网采集的难度要高很多, 所以, 要求我们的寻找信息的方式和采集信息的方式都应该极具灵活性, 以此达到理想的采集效果
在做数据采集之前最好能够确定自己采集的内容, 明确目标, 然后根据目标的展示形式去分析, 该网站是否会跳转其他的链接, 是否需要继续采集等各个因素
如果采集一个网站, 有时候也会考虑到网络重定向的问题, 例如我的个人博客原来链接是:
https://forestfdemo.github.io
, 但是我做了一个网络的重定向, 我将
https://chensenlin.cn
, 重定向到原来的链接, 这样无论是访问哪一个链接其本质都跳转到原来的链接, 此时需要了解的是网络重定向的时候, 我们采集的 URL 地址会发生变化, 在 Python3.X 的版本中, 我们的 urllib 会帮我完成重定向的功能
用 Scrapy 采集
Scrapy 就是一个可以大幅度降低我们对网页链接查找和识别工作复杂度的 Python 库, 它可以让你轻松地采集一个或多个域名的信息
目前 Scrapy 仅支持 Python 2.7, 还不支持 Python 3.x
利用 Scrapy 进行爬虫项目还是有一些设置, 我们可以采用官网下载, 也可以采用 pip 进行安装, 检验安装的方法在之前的 Python 基础系列的文章有提到过这里不做过多的阐述
创建一个项目:
- python scrapy startproject demourl
- New Scrapy project 'demourl', using template directory '/usr/local/lib/python3.6/site-packages/scrapy/templates/project', created in:
- /Users/demo/Project/python/demourl
- You can start your first spider with:
- cd demourl
- scrapy genspider example example.com
我们查看一下目录结构:
- demourl tree ./
- ./
- [drwxr-xr-x] demourl
- [-rw-r--r--] __init__.py
- [drwxr-xr-x] __pycache__
- [-rw-r--r--] items.py
- [-rw-r--r--] middlewares.py
- [-rw-r--r--] pipelines.py
- [-rw-r--r--] settings.py
- [drwxr-xr-x] spiders
- [-rw-r--r--] __init__.py
- [drwxr-xr-x] __pycache__
- [-rw-r--r--] scrapy.cfg
- 4 directories, 7 files
关于 Scrapy 的内容我们后续逐一介绍, 大家可以点击关注, 及时收到后续更新
博客原文链接: https://chensenlin.cn/posts/38026/
来源: http://www.jianshu.com/p/2fc3a3afbb1b