赞
踩
前面1-3小节就是基本功能
实现,完成了scrapy项目的创建和运行功能,能够爬取网络数据了,需要具备一定的Python基础和页面解析的能力
中间的4-6小节是部署工作
的内容,主要是实现定时任务,自动运行爬虫,可视化查看爬虫运行状态
最后的7-9小节是部署优化
工作的内容,基本上每个Python web应用甚至其他编程语言实现的Web应用都通用
文档
创建虚拟环境
# 创建虚拟环境
$ pyenv virtualenv 3.7.0 scrapy-env
# 切换环境
$ pyenv local scrapy-env
强烈建议
:不同的项目使用各自单独的虚拟环境来运行,确保不同项目的依赖库不冲突
文档
创建步骤
# 安装scrapy
$ pip install scrapy
# 查看版本
$ pip show scrapy
Version: 2.7.1
# 创建项目
$ scrapy startproject scrapy_demo
$ cd scrapy_demo
需要修改一些必要的配置
settings.py
# Obey robots.txt rules
# ROBOTSTXT_OBEY = True
ROBOTSTXT_OBEY = False
# 日志级别
# https://docs.scrapy.org/en/latest/topics/settings.html#std-setting-LOG_LEVEL
LOG_LEVEL = 'INFO'
# 创建爬虫
$ scrapy genspider baidu_spider www.baidu.com
爬虫文件示例
仅完成一个title标题的解析打印输出功能
scrapy_demo/scrapy_demo/spiders/baidu_spider.py
import scrapy from scrapy import cmdline from scrapy.http import HtmlResponse class BaiduSpiderSpider(scrapy.Spider): name = 'baidu_spider' allowed_domains = ['www.baidu.com'] start_urls = ['http://www.baidu.com/'] def parse(self, response: HtmlResponse, **kwargs): title = response.css('title::text').extract_first() print({'title': title}) if __name__ == '__main__': cmdline.execute("scrapy crawl baidu_spider".split())
运行爬虫
# 运行爬虫
$ scrapy crawl baidu_spider
文档
安装启动
pip install scrapyd
# 运行scrapyd,可以单独新建一个文件夹,作为运行目录
$ scrapyd
浏览器访问:http://127.0.0.1:6800/
最佳实践
方式一:
scrapyd 仅运行在本机,避免外网访问,仅通过 本机地址 和 可视化的工具访问
方式二:
如果需要外网访问,必须
配置用户名和密码,以免遭受到网络攻击
scrapyd.conf
[scrapyd]
username =
password =
文档
切换回项目根目录
$ pip install scrapyd-client
修改部署信息的配置文件
scrapy.cfg
[settings]
default = scrapy_demo.settings
[deploy:default]
url = http://localhost:6800/
project = scrapy_demo
部署
$ scrapyd-deploy
文档
pip install spider-admin-pro
# 启动
gunicorn 'spider_admin_pro.main:app'
浏览器访问:http://127.0.0.1:8000/
最佳实践:修改配置文件,设置用户名和密码
设置定时任务
Gunicorn文档:https://docs.gunicorn.org/
# 启动服务
$ gunicorn --config gunicorn.conf.py spider_admin_pro.run:app
注意:
一个配置示例:gunicorn.conf.py
# -*- coding: utf-8 -*- """ $ gunicorn --config gunicorn.conf.py spider_admin_pro.run:app """ import multiprocessing import os # 日志文件夹 LOG_DIR = 'logs' if not os.path.exists(LOG_DIR): os.mkdir(LOG_DIR) def resolve_file(filename): return os.path.join(LOG_DIR, filename) def get_workers(): return multiprocessing.cpu_count() * 2 + 1 # daemon = True daemon = False # 使用supervisor不能是后台进程 # 进程名称 proc_name = "spider-admin-pro" # 启动端口 bind = "127.0.0.1:5001" # 日志文件 loglevel = 'debug' pidfile = resolve_file("gunicorn.pid") accesslog = resolve_file("access.log") errorlog = resolve_file("error.log") # 启动的进程数 # workers = get_workers() workers = 1 worker_class = 'gevent' # 启动时钩子 def on_starting(server): ip, port = server.address[0] print('server.address:', f'http://{ip}:{port}')
文档:
spider-admin-pro.ini
[program: spider-admin-pro]
directory=/spider-admin-pro
command=/usr/local/python3/bin/gunicorn --config gunicorn.conf.py spider_admin_pro.run:app
stdout_logfile=logs/out.log
stderr_logfile=logs/err.log
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 0
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=0
一般线上环境都是通过Nginx来转发外部请求到内部地址
server { listen 80; server_name _; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { proxy_pass http://127.0.0.1:5001/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。