PyCharm+ENV+Scrapy

记录使用 PyCharm 创建 Scrapy 项目过程。

使用 PyCharm 创建一个干净的 Python 工程

使用 env 环境

1390107-75e9ae304040c9b9.png
Screen Shot 2019-01-15 at 9.55.59 AM.png

安装 Scrapy

打开 PyCharm 设置,安装 Scrapy。

1390107-2aa95aa962e1eb3f.png
Screen Shot 2019-01-15 at 9.41.06 AM.png

创建 Scrapy 工程

打开 PyCharm 命令行工具

# 后退一步,减少目录层级
cd  ..

# [Demo2] 必须和工程文件名相同
scrapy startproject Demo2

cd Demo2

# 使用模版创建一个爬虫
scrapy genspider example example.com

配置运行

创建 start_scrapy.py

from scrapy import cmdline
cmdline.execute('scrapy crawl example'.split())
1390107-8a242028816357cd.png
20190115102042.png

测试

修改 Demo2/spiders/example.py:

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

class ExampleSpider(scrapy.Spider):
    name = 'example'
    allowed_domains = ['baidu.com']
    start_urls = ['http://baidu.com/']

    def parse(self, response):
        print(response.body)

爬 baidu.com 还需要修改设置 settings.py ROBOTSTXT_OBEY = False

运行后:

1390107-4fe8089d07b583a8.png
20190115102211.png

其他问题

Forbidden by robots.txt

修改 settings.py:

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

参考:https://www.jianshu.com/p/eda047ac5c89

猜你喜欢

转载自blog.csdn.net/weixin_33834628/article/details/87011506
env