爬虫Scrapy练习-github项目

爬虫Scrapy练习

  • 简介
  • 训练内容
  • Unit 1 使用Scrapy爬取数据
    • 1.1 极其简单的spider
    • 1.2 优化:使用start_urls取代start_requests
    • 1.3 第一步页面处理并return
    • 1.4 使用yield取代return:
    • 1.5 爬取reddit,一个新闻网站

简介

爬虫训练网站
在学习了Scrapy入门之后,我们可以开始进一步通过实例练习,于是我找到了上面的训练项目,虽然没有教程,但是代码难度设置比较适合初学者训练。

我们从上面下载代码,开始学习。

训练内容

  • Unit 1: 使用Scrapy爬取数据
  • Unit 2: 通过Scaray浏览网页
  • Unit 3: 运行Spider在云端
  • Unit 4: 操作HTML形式
  • Unit 5: 爬取javascrpit形式
  • Unit 6:扩展爬虫

Unit 1 使用Scrapy爬取数据

1.1 极其简单的spider

爬取两个网站,记录爬取过的url。

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes1"

    def start_requests(self):
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        requests = []
        for url in urls:
            requests.append(scrapy.Request(url=url, callback=self.parse))
        return requests

    def parse(self, response):
        self.log('I just visited {}'.format(response.url))

1.2 优化:使用start_urls取代start_requests

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes2"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        self.log('I just visited {}'.format(response.url))

1.3 第一步页面处理并return

使用css处理选择到网页信息,然后返回
由于之前学习过了Scrapy框架的简单教程没什么难度就不详述了。

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes3"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        quotes = []
        for quote in response.css('div.quote'):
            quotes.append({
                'text': quote.css('span.text::text').extract_first(),
                'author': quote.css('span small::text').extract_first(),
                'tags': quote.css('div.tags a.tag::text').extract(),
            })
        # spider_4_quotes.py shows this same spider, but it generates
        # the items individually instead of returning all of them in list
        return quotes

其中返回的数据我们怎么获得呢?
在这里插入图片描述
返回的数据我们可以通过json来获得。
爬虫Scrapy练习-github项目_第1张图片
我们的控制台也能看到相应的信息:
爬虫Scrapy练习-github项目_第2张图片

1.4 使用yield取代return:

什么时候yield
主要是理解yield的应用:

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes4"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        for quote in response.css('div.quote'):
            yield {
                'text': quote.css('span.text::text').extract_first(),
                'author': quote.css('span small::text').extract_first(),
                'tags': quote.css('div.tags a.tag::text').extract(),
            }

1.5 爬取reddit,一个新闻网站

import scrapy
from datetime import datetime
class RedditSpider(scrapy.Spider):
    name = 'reddit'
    start_urls = [
        'http://reddit.com/r/programming',
        'http://reddit.com/r/python',
    ]

    def parse(self, response):
        # ':not(.stickied)' avoids scraping announcements that arte sticked to the top
        for thing in response.css('.thing:not(.stickied)'):
            yield {
                'title': thing.css('.title::text').extract_first(),
                'link': thing.css('.title > a::attr(href)').extract_first(),
                'user_name': thing.css('a.author::text').extract_first(),
                'user_url': thing.css('a.author::attr(href)').extract_first(),
                # when score is 0, reddit show a bullet point instead of a 0
                'score': int(thing.css('.score.unvoted::text').re_first('(\d+)') or 0),
                'time': datetime.strptime(
                    thing.css('time::attr(datetime)').extract_first(),
                    '%Y-%m-%dT%H:%M:%S+00:00'
                ),
            }

你可能感兴趣的:(Scrapy)