scrapy笔记
scrapy笔记
创建项目
scrapy startproject 项目名称
scrapy startproject httpbin_demo
看见下面输出, 就表示项目以创建好
You can start your first spider with:
    cd httpbin_demo
    scrapy genspider example example.com
此时项目虽然创建好,但是爬虫还是没有创建
需要使用以下命令来创建爬虫
cd httpbin_demo
scrapy genspider httpbin http://httpbin.org
此时控制台会输出以下内容
Created spider 'httpbin' using template 'basic' in module:
  httpbin_demo.spiders.httpbin
这个时候, 爬虫以创建好, 整个项目目录如下
httpbin_demo 
    scrapy.cfg # 这是配合scrapyd使用的配置文件, 一般不做可视化, 用不到
    httpbin_demo # 项目主目录
        __init__.py  
        items.py # 一般用来写数据结构
        middlewares.py # 中间件, 
        pipelines.py # 管道, 一般用于存储数据
        settings.py # 配置项
        spiders.py 
            __init__.py
            httpbin.py # 爬虫文件
一些命令
startproject 这个就是创建项目用的
genspider 用于生成爬虫, 提供了几个模板, 一般常用的就前两个
basic , crawl , csvfeed , xmlfeed
crawl 用于启动爬虫
scrapy crawl 爬虫名字, 这里爬虫名字要和生成的爬虫名字一样
check 用于检查代码是否有误
list 列出所有的爬虫
fetch url 会使用downloader 将网页源代码下载并在控制台展示出来
 scrapy fetch http://httpbin.org
view url 和fetch用法差不多, 只不过是将网页源代码在浏览器(文本文件)打开
settings -h 获取当前配置信息, 一般用不到~
runspider
这个也是启动爬虫, 但是和上面crawl 启动方式不一样
scrapy runspider 爬虫文件名称, 比如scrapy runspider ./spiders/httpbin.py

                
            
        
浙公网安备 33010602011771号