windows下搭建scrapy开发环境

python:python3.6

开发工具:intellij

1、安装scrapy 模块

pip3 install scrapy

2、创建项目目录和虚拟机

E:\work\proc>d:\Python\Python36\python.exe -m scrapy startproject compass

3、安装虚拟机

E:\work\proc>d:\Python\Python36\python.exe -m venv venv

开发scrapy

1、打开ide加载目录并指定ide的虚拟机目录为刚创建的venv目录即可。

2、创建requirements.txt里面写上scrapy按照提示安装scrapy模块和pypiwin32【windows下安装】。

 

3、编写代码

在spider包里新建一个toscrape-xpath.py:

# -*- coding: utf-8 -*-
import scrapy


class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
        'http://quotes.toscrape.com/',
    ]

    def parse(self, response):
        for quote in response.xpath('//div[@class="quote"]'):
            yield {
                'text': quote.xpath('./span[@class="text"]/text()').extract_first(),
                'author': quote.xpath('.//small[@class="author"]/text()').extract_first(),
                'tags': quote.xpath('.//div[@class="tags"]/a[@class="tag"]/text()').extract()
            }
        next_page_url = response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page_url is not None:
            yield scrapy.Request(response.urljoin(next_page_url))


if __name__ == '__main__':
    from scrapy import cmdline
    name = ToScrapeSpiderXPath().name
    cmd = 'scrapy crawl {0}'.format(name)
    cmdline.execute(cmd.split())

运行此文件即可:【默认日志为debug模式可以在settings.py的最后加上LOG_LEVEL = 'INFO'等模式】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值