Scrapy框架爬虫官网的学习

打印 上一主题 下一主题

主题 1604|帖子 1604|积分 4812

一、安装Scrapy框架

  1. pip install scrapy
复制代码
二、 创建scrapy框架默认目次

  1. scrapy startproject tutorial
  2. #tutorial为你的项目的名称
复制代码
该炒作会为您主动创建项目目次
三、项目目次介绍

  1. tutorial/
  2.     scrapy.cfg            # deploy configuration file
  3.     tutorial/             # project's Python module, you'll import your code from here
  4.         __init__.py
  5.         items.py          # project items definition file
  6.         middlewares.py    # project middlewares file
  7.         pipelines.py      # project pipelines file
  8.         settings.py       # project settings file
  9.         spiders/          # a directory where you'll later put your spiders
  10.             __init__.py
复制代码
四、先从第一只爬虫开始吧

爬虫用来界说爬取网站和相应结果处理是在Spider类界说的初始请求,我们开始第一次爬取数据新建一个文件在 tutorial/spiders 下的quotes_spider.py 
  1. import scrapy
  2. class QuotesSpider(scrapy.Spider):
  3.     #标记爬虫程序,他在项目是唯一的,不同的爬行器用不同的名称
  4.     name = "quotes"
  5.     #该方法必须返回一个请求的可迭代数据,爬虫会从第一个数据开始爬取
  6.     def start_requests(self):
  7.         urls = [
  8.             'http://quotes.toscrape.com/page/1/',
  9.             'http://quotes.toscrape.com/page/2/',
  10.         ]
  11.         for url in urls:
  12.             yield scrapy.Request(url=url, callback=self.parse)
  13.     #在每一个请求成功后处理相应数据的方法Response参数是 TextResponse 它保存页面内容
  14.     def parse(self, response):
  15.         page = response.url.split("/")[-2]
  16.         filename = f'quotes-{page}.html'
  17.         with open(filename, 'wb') as f:
  18.             f.write(response.body)
  19.         self.log(f'Saved file {filename}')
复制代码
怎样运行我们的爬虫代码呢?
跳转到项目顶级目次下运行:
  1. scrapy crawl quotes
  2. #quotes为爬虫文件的名称
复制代码
代码会生成两个文件,雷同于这样:、

这种方式大概不是那么快捷,有些操纵是没有必要的,我们可以直接这样写
  1. from pathlib import Path
  2. import scrapy
  3. class QuotesSpider(scrapy.Spider):
  4.     name = "quotes"
  5.     #变量名为start_urls  会默认实现start_requests方法,无需书写该方法
  6.     start_urls  = [
  7.         "https://quotes.toscrape.com/page/1/",
  8.         "https://quotes.toscrape.com/page/2/",
  9.     ]
  10.     def parse(self, response):
  11.         page = response.url.split("/")[-2]
  12.         filename = f"quotes-{page}.html"
  13.         with open(filename, 'wb') as f:
  14.             f.write(response.body)
复制代码
个人理解就是省却了写一个方法通过命名变量的方式来默认实现 start_urls  方法
五、提取数据

最好的学习方式是利用shell举行学习
在终端上运行:
  1. scrapy shell "http://quotes.toscrape.com/page/1/"
复制代码
 你可以看到这样的结果:

你可以尝试筛选响应的对象:
  1. response.css('title')
  2. #该操作为使用scrapy库里的CSS选择器来筛选数据
复制代码
执行之后你可以看到这样的响应
  1. [<Selector query='descendant-or-self::title' data='<title>Quotes to Scrape</title>'>]
  2. #这个操作代表获取到了一个HTML中的 <title> 标签,内容为 Quotes to Scrape
  3. #Scrapy 返回的是一个包含 Selector 对象的列表
复制代码
那么怎么提取其中的文本呢?
  1. response.css('title::text').getall()
复制代码
结果为:
  1. ['Quotes to Scrape']
复制代码
即 ::text会筛选出title标题中的数据,假如我们不指定text那么会获取到完整的title标签:
  1. >>> response.css('title').getall()      
  2. ['<title>Quotes to Scrape</title>']
复制代码
 同时值得你注意的是,getall()返回的数据是一个列表,假如你只要返回一个数据可以
  1. >>> response.css('title').get()        
  2. '<title>Quotes to Scrape</title>'
复制代码
同时也可以有替代写法:
  1. >>> response.css('title')[0].get()   
  2. '<title>Quotes to Scrape</title>'
复制代码
 
 
 
 
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表