Scrapy:Hello World:修订间差异

来自WHY42
imported>Soleverlee
(以“=创建项目= <source lang="bash"> scrapy startproject riguz </source> 生成的项目如下: <pre> E:. │ scrapy.cfg │ └─riguz │ items.py │...”为内容创建页面)
 
imported>Soleverlee
 
第42行: 第42行:
<source lang="bash">
<source lang="bash">
cd riguz
cd riguz
scrapy crawl helloworld
scrapy crawl helloworld -s LOG_FILE=scrapy.log 
</source>
</source>
运行结果为两个下载的页面:
运行结果为两个下载的页面:

2017年1月11日 (三) 06:39的最新版本

创建项目

scrapy startproject riguz

生成的项目如下:

E:.
│  scrapy.cfg
│
└─riguz
    │  items.py
    │  middlewares.py
    │  pipelines.py
    │  settings.py
    │  __init__.py
    │
    └─spiders
            __init__.py

创建爬虫

在spiders文件夹下面新建一个文件,例如hello_spider.py:

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "helloworld"
    
    def start_requests(self):
        urls = [
            'https://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)
    def parse(self, response):
        page = response.url.split("/")[-2]
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

运行

cd riguz
scrapy crawl helloworld -s LOG_FILE=scrapy.log

运行结果为两个下载的页面:

│  quotes-1.html
│  quotes-2.html
│  scrapy.cfg