Scrapy 项目:QuotesBot
QuotesBot
This is a Scrapy project to scrape quotes from famous people from http://quotes.toscrape.com (github repo).
This project is only meant for educational purposes.

任务:
爬取该网站的名人名言、作者、作者信息(名字,生日、描述)以及名言标签,并保存
import scrapy
import re class AuthorSpider(scrapy.Spider):
name = "author"
start_urls = ["http://quotes.toscrape.com/"] def parse(self, response):
author_page_links = response.css('.author + a')
yield from response.follow_all(author_page_links, self.parse_author) next_page_links = response.css('li.next a')
yield from response.follow_all(next_page_links, self.parse) def parse_author(self, response):
def extract_with_css(query):
return response.css(query).get(default="").strip() yield {
"name": extract_with_css("h3.author-title::text"),
"birthdate": extract_with_css(".author-born-date::text"),
"bio": extract_with_css(".author-description::text"),
}
保存:
scrapy crawl spidername -o test.csv
项目练习:
Extracted data
This project extracts quotes, combined with the respective author names and tags. The extracted data looks like this sample:
{
'author': 'Douglas Adams',
'text': '“I may not have gone where I intended to go, but I think I ...”',
'tags': ['life', 'navigation']
}
Spiders
This project contains two spiders and you can list them using the list command:
$ scrapy list
toscrape-css
toscrape-xpath
Both spiders extract the same data from the same website, but toscrape-css employs CSS selectors, while toscrape-xpath employs XPath expressions.
You can learn more about the spiders by going through the Scrapy Tutorial.
Running the spiders
You can run a spider using the scrapy crawl command, such as:
scrapy crawl toscrape-css
If you want to save the scraped data to a file, you can pass the -o option:
scrapy crawl toscrape-css -o quotes.json
项目代码:
class QuotesbotSpider(scrapy.Spider):
name = "quotesbot"
start_urls = ["http://quotes.toscrape.com"] def parse(self, response, **kwargs):
for quote in response.css('div.quote'):
yield {
"author":quote.css(".author::text").get(),
"text":quote.css(".text::text").get(),
"tags":quote.css(".tags meta::attr(content)").get(),
} next_page_link = response.css("li.next a")
if next_page_link is not None:
yield from response.follow_all(next_page_link, callbac
结果:

Scrapy 项目:QuotesBot的更多相关文章
- 亲测——pycharm下运行第一个scrapy项目 ©seven_clear
最近在学习scrapy,就想着用pycharm调试,但不知道怎么弄,从网上搜了很多方法,这里总结一个我试成功了的. 首先当然是安装scrapy,安装教程什么的网上一大堆,这里推荐一个详细的:http: ...
- scrapy(一)建立一个scrapy项目
本项目实现了获取stack overflow的问题,语言使用python,框架scrapy框架,选取mongoDB作为持久化数据库,redis做为数据缓存 项目源码可以参考我的github:https ...
- Python Scrapy项目创建(基础普及篇)
在使用Scrapy开发爬虫时,通常需要创建一个Scrapy项目.通过如下命令即可创建 Scrapy 项目: scrapy startproject ZhipinSpider 在上面命令中,scrapy ...
- pycharm创建scrapy项目教程及遇到的坑
最近学习scrapy爬虫框架,在使用pycharm安装scrapy类库及创建scrapy项目时花费了好长的时间,遇到各种坑,根据网上的各种教程,花费了一晚上的时间,终于成功,其中也踩了一些坑,现在整理 ...
- 【Python3爬虫】第一个Scrapy项目
Python版本:3.5 IDE:Pycharm 今天跟着网上的教程做了第一个Scrapy项目,遇到了很多问题,花了很多时间终于解决了== 一.Scrapy终端(scrapy shell) Sc ...
- eclipse创建scrapy项目
1. 您必须创建一个新的Scrapy项目. 进入您打算存储代码的目录中(比如否F:/demo),运行下列命令: scrapy startproject tutorial 2.在eclipse中创建一个 ...
- python爬虫scrapy项目详解(关注、持续更新)
python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...
- Scrapy项目创建以及目录详情
Scrapy项目创建已经目录详情 一.新建项目(scrapy startproject) 在开始爬取之前,必须创建一个新的Scrapy项目.进入自定义的项目目录中,运行下列命令: PS C:\scra ...
- Scrapy项目结构分析和工作流程
新建的空Scrapy项目: spiders目录: 负责存放继承自scrapy的爬虫类.里面主要是用于分析response并提取返回的item或者是下一个URL信息,每个Spider负责处理特定的网站或 ...
- 爬虫系列2:scrapy项目入门案例分析
本文从一个基础案例入手,较为详细的分析了scrapy项目的建设过程(在官方文档的基础上做了调整).主要内容如下: 0.准备工作 1.scrapy项目结构 2.编写spider 3.编写item.py ...
随机推荐
- codeblocks下载安装及快捷键
100MB的下载链接:自带mingw http://pan.baidu.com/s/1o6BgFP4 13.12版本 gcc 4.7.1的 这是windows版本的 linux下编译安装:参考: ...
- centos /usr/local 和/opt 安装软件你什么不同../configure --prefix=/usr...
/usr/local下一般是你安装软件的目录,这个目录就相当于在windows下的programefiles这个目录(所有文件在一个文件夹) /opt这个目录是一些大型软件的安装目录,或者是一些服务程 ...
- 32.vsftpd服务程序--匿名开放模式
1.vsftpd服务程序 vsftpd 作为更加安全的文件传输的服务程序,允许用户以三种认证模式登录到FTP 服务器上. 匿名开放模式:是一种最不安全的认证模式,任何人都可以无需密码验证而直接登录到F ...
- 肝了一个半月的 Java 项目快速开发脚手架:Chewing
前言 闲来无事,整一个 Java 项目快速开发脚手架. 正文 一.简介 Chewing 是一个简单的 Java 项目快速开发脚手架.既适合需要开发小型项目的小伙伴使用,也适合刚入门的新手用来学习一些常 ...
- HTML学习速记
HTML结构导图 先看HTML结构示意图 本文主要讲解HTML当中的一些html标签,css样式,javascript方法 html html的标签有各式各样的数不胜数,但是常用的并不多,总结下我目前 ...
- DolphinScheduler1.3.2源码分析(一)看源码前先把疑问列出来
1.谈谈如何看源码 个人觉得拿到一个开源的项目,首先要先使用一下. 如果是有页面的那种,可以先把测试平台部署起来,然后到处随意点点,然后用一下最基础的功能,走一遍基础的使用流程.不用担心会把系统弄 ...
- CCF计算机软件能力认证试题练习:201912-5 魔数
CCF计算机软件能力认证试题练习:201912-5 魔数 前置知识:BFS,线段树等 \(f(x) = (x\%A)\%B\) 这个函数值的和直接用线段树维护是不太行的(也可能是我不知道),后来想了很 ...
- 动态规划TG.lv(1) (洛谷提高历练地)
动态规划TG.lv(1) P1005 矩阵取数游戏 分析:每行不超过80个数字,直接区间DP即可,\(dp[i][j]\)表示区间\([i,j]\)之间取数可以得到的答案,每次向右或者向左扩展即可.但 ...
- hdu5627 Clarke and MST (并查集)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- SpringBoot简单整合redis
Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...
