Intelij idea 集成scrapy开发环境

来源:互联网 发布:网络机柜的作用 编辑:程序博客网 时间:2024/06/05 17:03

以MacOs sierra0.12.6 为例

1.安装Intelij idea

下载地址 https://www.jetbrains.com/idea/download/#section=mac

2.安装idea 支持 python 开发的插件

Preferences -> Plugins
这里写图片描述

3.mac 神器homebrew(已经安装好pip的同学,这一步请忽略)

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

4.安装配置pip

brew install pip

安装pip之后最重要的配置就是配置pip的源,什么是源呢。。。。
就是各种插件的下载地址啦

mkdir ~/.pipvi ~/.pip/pip.conf

在pip.conf文件填入下列配置保存就好了

[global]index-url = https://pypi.douban.com/simple

楼主这里用的是豆瓣的源
很多文章里面写的index-url = http://pypi.douban.com/simple,这是不对的,现在各大网站都换成了https,哪还有http的,不够安全。
国内的源还有
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:https://pypi.hustunique.com/
山东理工大学:https://pypi.sdutlinux.org/
豆瓣:https://pypi.douban.com/simple/

5.安装Scrapy

sudo pip install Scrapy --upgrade --ignore-installed six

注意(大坑在此)
后面添加的参数一定要加上–upgrade –ignore-installed six
因为mac自己本身就已经安装了six包,但是因为系统安全的原因,这个包的权限非常高,sudo都不能执行更新,如果执行pip install Scrapy不忽略six包就会报错。

DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version.

这里写图片描述

6.idea和Scrapy结合

创建一个Scrapy项目,如果对Scrapy项目不熟悉的小伙伴可以参看Scrapy的文档https://doc.scrapy.org/en/0.16/intro/tutorial.html
这里我们就使用文档里的介绍来创建一个最简单

scrapy startproject tutorial

使用idea打开tutorial项目
根据官网文档,我们创建一个最简单的爬虫

import scrapy;class QuotesSpider(scrapy.Spider):    name = "quotes"    def start_requests(self):        urls = [            'http://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)

然后在右上角配置运行环境 Edit Configurations…
这里写图片描述
这里写图片描述
Script选择项目的__init__.py文件
然后修改__init__.py文件

from scrapy import cmdlinecmdline.execute("scrapy crawl quotes".split());

这样idea就跟Scrapy完美结合了,运行,断点debug,代码分析不在话下。这些功能都在右上角这么些个按钮了。各位同学自己慢慢去试吧。
这里写图片描述

原创粉丝点击