怎么在 PyCharm 里编写 Python pyspider 的代码

[@binux](/user/binux)

pyspider 很好用,但是有一点缩手缩脚的是在网页里写代码。

缩进还好说,毕竟多按几次 tab 对比一下就行了,但是代码自动补全就不行了,像我这种刚开始玩 python 的人,没有 IDE 简直不能活啊。。 IDE 写 10 行代码的功夫,在网页里才写 1 行。

所以问下,有没有配置的教程,像How to use PyCharm to debug Scrapy projects一样


怎么在 PyCharm 里编写 Python pyspider 的代码

11 回复

在PyCharm里写pyspider代码和写普通Python项目差不多,主要就是配置好环境和项目结构。

首先确保你装了pyspider:

pip install pyspider

然后在PyCharm里新建Python项目,创建你的爬虫文件,比如my_spider.py

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
        'headers': {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('http://example.com', callback=self.index_page)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('a[href^="http"]').items():
            self.crawl(each.attr.href, callback=self.detail_page)

    def detail_page(self, response):
        return {
            "url": response.url,
            "title": response.doc('title').text(),
        }

运行的话,你需要先启动pyspider的web界面和调度器:

pyspider

然后在浏览器打开http://localhost:5000,在web界面里创建项目并运行。

如果你想让爬虫在PyCharm里直接运行,可以创建一个运行配置,但说实话pyspider主要还是通过它的web界面来管理和监控爬虫的。

总结:配置好环境,按pyspider的Handler类结构写代码就行。


请问 windows 咋整

#3 windows 也有东西支持 webdav 的吧

python3 无法用 webdav ???

#6 可以吧

楼主请问你的解决了么

#8 v0.3.9 支持了啊

我用的是 2.2.1 ,没法用……

回到顶部