[爬虫] 学Scrapy,顺便把它的官方教程给爬下来
想学爬虫主要是因为算法和数据是密切相关的,有数据之后可以玩更多有意思的事情,数据量大可以挖掘挖掘到更多的信息。
之前只会通过python中的request库来下载网页内容,再用BeautifulSoup、re正则工具来解析;后来了解到Scrapy爬虫框架,现在入门先写个小小的爬虫项目,这里做个简单的总结和记录。
官方教程:https://scrapy-chs.readthedocs.io/zh_CN/1.0/intro/overview.html(包括安装指南)
Github:https://github.com/scrapy
1. 创建项目
scrapy startproject -h
scrapy startproject scrapytutorial
cd scrapytutorial/
scrapy genspider scrapy_tutorial_spider scrapy-chs.readthedocs.io
mkdir output
2. 编写爬虫代码
# -*- coding: utf-8 -*-
import scrapy
import codecs class ScrapyTutorialSpiderSpider(scrapy.Spider):
name = 'scrapy_tutorial_spider'
# allowed_domains = ['scrapy-chs.readthedocs.io']
start_urls = ['https://scrapy-chs.readthedocs.io/zh_CN/1.0/intro/overview.html'] def parse(self, response):
print("response.url: %s" % response.url) # 保存完整网页内容到文件
filename = response.url.split("/")[-1]
print("filename: %s" % filename)
with codecs.open("output/" + filename, "wb") as fw:
fw.write(response.body) # TODO 提取关键信息 # 遍历下一页
next_url = response.css("div.rst-footer-buttons > a::attr('href')").extract()[0]
if next_url is not None:
next_url = response.urljoin(next_url)
print("next_url: %s" % next_url)
yield scrapy.Request(next_url)
3. 启动爬取
scrapy crawl scrapy_tutorial_spider
完整爬下来有45个文件:
因为刚上手,先按下面几步走:
(1) 把某个网页完整爬下来,保存到文件
(2) 追踪链接:通过提取感兴趣的页面的链接(例如想要下一页的内容)并进行追踪,获取更多的数据
(3) 动态解析网页,只提取感兴趣的部分内容并保存
目前还不太熟悉CSS选择器以及XPath表达式,关于第(3)部提取关键信息还没做,后续将会逐渐学习和完善。
(参考官网的两个例子:tutorial、QuotesBot)
另外,网上有很多不错的爬虫项目,可以用来练手:32个Python爬虫项目
爬虫可能涉及到定时爬取、账号注册和登录、验证码破解等等,还是挺有挑战性的~
[爬虫] 学Scrapy,顺便把它的官方教程给爬下来的更多相关文章
- 小白学 Python 爬虫(33):爬虫框架 Scrapy 入门基础(一)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(41):爬虫框架 Scrapy 入门基础(八)对接 Splash 实战
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(34):爬虫框架 Scrapy 入门基础(二)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(37):爬虫框架 Scrapy 入门基础(五) Spider Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(38):爬虫框架 Scrapy 入门基础(六) Item Pipeline
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(40):爬虫框架 Scrapy 入门基础(七)对接 Selenium 实战
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 爬虫之scrapy框架
解析 Scrapy解释 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓 ...
随机推荐
- #ifdef和#if defined的差别
注意两者都有个define的作用,区别在于使用方式上.前者的通常用法是:#ifdef XXX .... #else .... #endif 只能在两者中选择是否有定义.对于后者,常用法是: #if ...
- python3 备份mysql小程序
为了保证数据安全,一般都会定期备份数据库,备份数据库也有自己的命令可以执行,下面就是一个每天备份mysql数据库的一个小程序. mysql备份的命令如下: mysqldump -uroot -p123 ...
- h5前端项目常见问题汇总
原文作者:FrontEndZQ 原文链接:https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度 ...
- android ReactNative之Cannot find entry file index.android.js in any of the roots
android ReactNative之Cannot find entry file index.android.js in any of the roots 2018年04月02日 14:53:12 ...
- Linq Query常见错误
1.只能对 Type.IsGenericParameter 为 True 的类型调用方法 对于此错误,一般常见在虚拟实体,但是要把条件拼接在Expression中,通常是因为该字段在数据库中是可空的, ...
- Codeforces Round #532 (Div. 2) Solution
A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...
- MySql如何安装?
官方网址:https://www.mysql.com/downloads/ Community——>MySqlCommunity Server->GA->64位: GA:正式版: C ...
- RC522 模块驱动程序
本文主要讲述了基于SPI总线的RC522驱动程序的设计.描述了主控如何与从设备通过SPI总线进行数据的读写. 一 在SPI驱动中,有两个重要的结构:spi_device&spi_driver. ...
- target='_blank' 安全漏洞
有关 target="_blank" 的安全缺陷 可能大家在写网页的时候经常给超链接加个属性 target="_blank",意思就是在浏览器新的窗口打开此超链 ...
- firefox_flash_install_on_kali
手动安装firefox的flash的步骤 1> 下载flash的tar.gz安装包 firefox http://get2.adobe.com/cn/flashplayer/otherversi ...