scrapy - zixcon/python GitHub Wiki

  1. https://docs.scrapy.org/en/latest/intro/tutorial.html

1. startproject
scrapy startproject ch1
2. genspider
$ scrapy genspider -l
Available templates:
basic
crawl
csvfeed
xmlfeed

$ scrapy genspider example example.com Created spider ‘example’ using template ‘basic’ $ scrapy genspider -t crawl scrapyorg scrapy.org Created spider ‘scrapyorg’ using template ‘crawl’

3. scrapy crawl quotes
scrapy crawl quotes -o quotes.json
scrapy crawl quotes -o quotes.jl

4. runspider
scrapy runspider myspider.py

  1. 使用参照:
    http://www.pythonsite.com/?p=235

XPath选择器
常用的路径表达式,这里列举了一些常用的,XPath的功能非常强大,内含超过100个的内建函数。
下面为常用的方法

nodeName 选取此节点的所有节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,不考虑它们的位置
. 选择当前节点
.. 选取当前节点的父节点
@ 选取属性

  • 匹配任何元素节点
    @* 匹配任何属性节点
    Node() 匹配任何类型的节点
    CSS选择器
    CSS层叠样式表,语法由两个主要部分组成:选择器,一条或多条声明
    Selector {declaration1;declaration2;……}

下面为常用的使用方法

.class .color 选择class=”color”的所有元素
#id #info 选择id=”info”的所有元素

  • * 选择所有元素
    element p 选择所有的p元素
    element,element div,p 选择所有div元素和所有p元素
    element element div p 选择div标签内部的所有p元素
    [attribute] [target] 选择带有targe属性的所有元素
    [arrtibute=value] [target=blank] 选择target=”blank”的所有元素
    选择器的使用例子
  1. 定时爬取
    http://blog.csdn.net/qq_33042187/article/details/79023099
    import time
    import os
    while True:
    print(‘the first spider’)
    os.system(“scrapy crawl human -o human.json”)
    print(‘the second spider’)
    os.system(“scrapy crawl nbgov -o nbgov.json”)
    time.sleep(86400)# 24hours
⚠️ **GitHub.com Fallback** ⚠️