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的更多相关文章

  1. 亲测——pycharm下运行第一个scrapy项目 ©seven_clear

    最近在学习scrapy,就想着用pycharm调试,但不知道怎么弄,从网上搜了很多方法,这里总结一个我试成功了的. 首先当然是安装scrapy,安装教程什么的网上一大堆,这里推荐一个详细的:http: ...

  2. scrapy(一)建立一个scrapy项目

    本项目实现了获取stack overflow的问题,语言使用python,框架scrapy框架,选取mongoDB作为持久化数据库,redis做为数据缓存 项目源码可以参考我的github:https ...

  3. Python Scrapy项目创建(基础普及篇)

    在使用Scrapy开发爬虫时,通常需要创建一个Scrapy项目.通过如下命令即可创建 Scrapy 项目: scrapy startproject ZhipinSpider 在上面命令中,scrapy ...

  4. pycharm创建scrapy项目教程及遇到的坑

    最近学习scrapy爬虫框架,在使用pycharm安装scrapy类库及创建scrapy项目时花费了好长的时间,遇到各种坑,根据网上的各种教程,花费了一晚上的时间,终于成功,其中也踩了一些坑,现在整理 ...

  5. 【Python3爬虫】第一个Scrapy项目

    Python版本:3.5    IDE:Pycharm 今天跟着网上的教程做了第一个Scrapy项目,遇到了很多问题,花了很多时间终于解决了== 一.Scrapy终端(scrapy shell) Sc ...

  6. eclipse创建scrapy项目

    1. 您必须创建一个新的Scrapy项目. 进入您打算存储代码的目录中(比如否F:/demo),运行下列命令: scrapy startproject tutorial 2.在eclipse中创建一个 ...

  7. python爬虫scrapy项目详解(关注、持续更新)

    python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...

  8. Scrapy项目创建以及目录详情

    Scrapy项目创建已经目录详情 一.新建项目(scrapy startproject) 在开始爬取之前,必须创建一个新的Scrapy项目.进入自定义的项目目录中,运行下列命令: PS C:\scra ...

  9. Scrapy项目结构分析和工作流程

    新建的空Scrapy项目: spiders目录: 负责存放继承自scrapy的爬虫类.里面主要是用于分析response并提取返回的item或者是下一个URL信息,每个Spider负责处理特定的网站或 ...

  10. 爬虫系列2:scrapy项目入门案例分析

    本文从一个基础案例入手,较为详细的分析了scrapy项目的建设过程(在官方文档的基础上做了调整).主要内容如下: 0.准备工作 1.scrapy项目结构 2.编写spider 3.编写item.py ...

随机推荐

  1. hadoop知识点总结(三)YARN设计理念及基本架构

    YARN设计理念与基本架构 1,MRv1的局限性:扩展性差,可靠性差,资源利用率低,无法支持多种计算框架 2,YARN基本设计思想 1)基本框架对比 Hadoop1.0中,JobTracker由资源管 ...

  2. Linux常用习惯和技巧

    1.如果有些命令在执行时不断地在屏幕上输出信息,影响到后续命令的输入,则可以在执行命令时在末尾添加上一个&符号,这样命令将进入系统后台来执行.

  3. Linux-apache httd.conf文件详解

    Linux-apache httd.conf文件详解 # This is the main Apache server configuration file. It contains the # co ...

  4. 封装打包Python脚本

    1.前言 封装打包Python的好处,节省了安装各种各样包依赖的问题,同时可以加强我们代码隐私的安全性,这里我的演示环境是Python3.6 ,CentOS7的系统,同时打包工具采用pyinstall ...

  5. Flink-v1.12官方网站翻译-P009-Event-driven Applications

    事件驱动的应用 处理函数 简介 ProcessFunction将事件处理与定时器和状态结合起来,使其成为流处理应用的强大构件.这是用Flink创建事件驱动应用的基础.它与RichFlatMapFunc ...

  6. 翻译:《实用的Python编程》01_01_Python

    目录 | 下一节 (1.2 第一个程序) 1.1 Python Python 是什么? Python 是一种解释型(译者注:区别于编译型)的高级语言, 通常被归类为 "脚本语言"  ...

  7. cmath取整函数

    #include <iostream> #include <cmath>//头文件 using namespace std; int main () { double n; c ...

  8. python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值

    Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...

  9. HDU 3032 Nim or not Nim?(SG打表找规律)

    题意: 给你n堆石子,你每次只能操作一堆石子 1.拿去任意个,最少1个 2.把这一堆分成两堆,没有要求对半分 解析+代码: 1 //解题思路: 2 //对于一个给定的有向无环图,定义关于图的每个顶点的 ...

  10. PowerShell随笔3 ---别名

    上一篇提到了别名,这个有必要说一下,因为我们常常会遇到以下两种情况: 自己写脚本,想快速一些,使用命名 看别人的脚本,发现别人和你想的一样,用了别名,但是你忘记了这个别名是什么意思. 我们可以通过Ge ...