开发环境
- Python第三方库:lxml、Twisted、pywin32、scrapy
- Python 版本:python-3.5.0-amd64
- PyCharm软件版本:pycharm-professional-2016.1.4
- 电脑系统:Windows 10 64位
如果你还没有搭建好开发环境,请到这篇博客。
1 知识点:scrapy 爬虫项目的创建及爬虫的创建
1.1 scrapy 爬虫项目的创建
接下来我们为大家创建一个Scrapy爬虫项目,并在爬虫项目下创建一个Scrapy爬虫文件。
scrapy startproject <projectname>
1.2 scrapy 爬虫文件的创建
cd demo
scrapy genspider -t basic <filename> <domain>
更多 Scrapy 命令的介绍请到这篇博客查看。
2 实例:爬取百度标题和CSDN博客
我们创建一个爬虫项目,在里面创建一个爬虫文件来爬取百度,并再创建一个爬虫文件爬取CSDN博客文章。
先创建一个Scrapy爬虫项目:
scrapy startproject firstDemo
输出:
D:\WorkSpace\python_ws\python-large-web-crawler>scrapy startproject firstdemo
New Scrapy project 'firstdemo', using template directory 'c:\\users\\aobo\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\scrapy\\templates\\project', created in:
D:\WorkSpace\python_ws\python-large-web-crawler\firstdemo
You can start your first spider with:
cd firstdemo
scrapy genspider example example.com
D:\WorkSpace\python_ws\python-large-web-crawler>
2-1.1 使用Scrapy爬虫 爬取百度标题
创建一个爬虫文件来爬取百度
cd firstDemo
scrapy genspider -t basic baidu baidu.com
输出:
D:\WorkSpace\python_ws\python-large-web-crawler>cd firstdemo
D:\WorkSpace\python_ws\python-large-web-crawler\firstdemo>scrapy genspider -t basic baidu baidu.com
Created spider 'baidu' using template 'basic' in module:
firstdemo.spiders.baidu
D:\WorkSpace\python_ws\python-large-web