一、爬虫技术概述
二、爬虫的实现
很多语言都可以用来开发爬虫,理论上只要有访问 络能力的语言都可以用来开发爬虫。但是目前主流的爬虫开发用的主要是python语言。python语言简单、清晰、高效开发的特点特别适合于爬虫这种需要根据不同页面灵活高效开发不同处理程序的需求的场景。
三、Python爬虫框架
- Python爬虫框架介绍
利用Pythong的基础 络包开发爬虫是比较麻烦的,市面上有很多基于这套API开发的爬虫框架,大大的简化了Python爬虫的开发
比较知名的是Scrapy 和PySpider。
PySpider上手更简单,操作更加简便,增加了 WEB 界面,开发效率高,集成了phantomjs,可以用来处理复杂的js渲染的页面。(可定制的能力减弱)
Scrapy自定义程度高,比 PySpider更底层一些,适合学习研究,需要学习的相关知识多,不过自己拿来研究分布式和多线程等等是非常合适的。
2.PySplider安装配置
(1)安装pip
a. pip是python的包管理工具 类似RedHat里面的yum ,通过pip可以快速下载安装python软件,隐藏了背后复杂的下载安装过程。
b. 访问https://pypi.python.org/pypi/pip#downloads
c. 下载pip安装包
d. 解压pip-9.0.1.tar.gz
e. 在该目录下执行 python setup.py install
f. 将python安装目录下的Scripts配置到PATH环境变量
(2) 安装phantomjs
phantomjs是一个浏览器内核程序,可以在爬虫开发过程中,模拟浏览器
访问http://phantomjs.org/download.html
下载对应版本phantomjs
解压phantomjs-2.1.1-windows.zip
将其bin目录配置到PATH环境变量中
(3)使用pip安装pyspider
执行pip install pyspider
(4)运行pyspider
执行pyspider all
(5)通过浏览器访问pyspider
http://localhost:5000
ef on_start(self) is the entry point of the script. It will be called when you click the run button on dashboard. def
on_start(self)是脚本的入口。当你点击run按钮的时候会调用它。 elf.crawl(url,
callback=self.index_page) is the most important API here. It will add
a new task to be crawled. Most of the options will be spicified via
self.crawl arguments.
这个方法是一个最重要的API。他会增加一个新的爬取任务。大部分的选项可以通过self.crawl参数进行指定。 ef
index_page(self, response) get a Response* object. response.doc* is a
pyquery object which has jQuery-like API to select elements to be
extracted. def index_page(self,response)会得到一个respponse对象。response.doc是
pyquery类型的对象,具有类似jQuery的API选择要被提取的元素。 ef detail_page(self, response)
return a dict object as result. The result will be captured into
resultdb by default. You can override on_result(self, result) method
to manage the result yourself.
default_page返回结果是一个字典对象。这个结果将被默认放到resultdb中。你可以按照自己的意愿重写覆盖on_result方法管理得到的结果。 every(minutes=2460, seconds=0) is a helper to tell the scheduler
that on_start method should be called everyday.
这个注解帮助告诉工程调度:on_start方法每天都被调用。 config(age=10 * 24 * 60 * 60)*
specified the default ageparameter of self.crawl with page type
index_page (when callback=self.index_page). The parameter age* can be specified via self.crawl(url, age=10246060) (highest priority) and
crawl_config (lowest priority).
ge=10 * 24 * 60 * 60 tell scheduler
discard the request if it have been crawled in 10 days. pyspider will not crawl a same URL twice by default (discard forever), even you had modified the code, it’s very common for beginners that runs the
project the first time and modified it and run it the second time, it will not crawl again (read itag for solution) 它告诉调度器忽略这个请求,如果它十天之内爬取过。
config(priority=2)* mark that detail pages should be crawled first.
优先级高的 页首先被爬取。*
Before Start:
eb is a system of interlinked hypertext pages.
络是链接起来的超文本页面组成的系统
ages is identified on the Web via uniform resource locator (URL).
页是通过URL地址唯一标识的
ages transferred via the Hypertext Transfer Protocol (HTTP).
面通过HTTP进行传输
eb Pages structured using HyperText Markup Language (HTML).
面是通过HTML语言编写的
To scrape information from a web is 从一个 页中爬取的信息可以是:
1.Finding URLs of the pages contain the information we want.
找到包含我们想要信息的页面们的地址
2.Fetching the pages via HTTP.
获取页面们通过HTTP协议
3.Extracting the information from HTML.
筛选出HTML页面中的信息
4.Finding more URL contains what we want, go back to 2.
找到更多想要爬取的URL地址,回到第二步。
Pick a start URL
As we want to get all of the movies on IMDb, the first thing is finding a list. A good list page may:
想要爬取 IMDb的所有电影,需要一个列表,一个好的列表可能是这样的:
ontaining links to the movies as many as possible.
尽可能的包含更多的电影的链接。
y following next page, you can traverse all of the movies.
通过点击下一页,可以访问所有的电影。
ist sorted by last updated time would be a great help to get latest movies.
列表可以通过最新的时间排序将会提供很大的帮助。
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!