shell 调试 格式 scrapy shell url
1 2 scrapy shell https://www.9ku.com/geshou/798/info.htm
scrapy 创建项目 1 2 3 4 5 6 7 8 #  创建项目 #  scrapy startproject scrapy_baidu_091 #  创建爬虫文件, 需要进入spiders目录 #  scrapy genspider baidu www.baidu.com #  运行爬虫文件 #  scrapy crawl baidu 
1 2 3 4 5 6 7 8 9  scrapy shell https://www.dushu.com/book/1611.html
parse(self, response) 获取网页源码, 二进制 , xpath 的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 def parse(self, response):
当我们直接使用 xpath 获取元素时, 发现打印出来的是 Selector 标签, 并不是我们想要的
1 2 3 4 5 6 def parse(self, response):
1 2 3 4 <Selector xpath="//div[@class='songName']/a[@class='songNameA']//text()" data='晴天'>
加入把以上 extract(), 代码修改成下面代码
1 2 3 4 5 6 def parse(self, response):
1 2 3 4 晴天
extract()是从标签中获取数据, ]列表中获取第一个数据   
向管道传输数据, 管道下载数据 1 2 3 4 5 6 
items.py
1 2 3 4 5 6 7 8 9 10 11 class ScrapyDangdang095Item(scrapy.Item):
管道 pipelines.py 下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class ScrapyDangdang095Pipeline:#  多条管道开启 #  (1) 定义管道类 #  (2) 在settings中开启管道 #     "scrapy_dangdang_095.pipelines.DangDangDownloadPipeline" : 301, 
yield 调用函数 自己调用自己
1 2 3  def parse(self, response):
调用其他函数, 并且携带 meta 参数, 接收参数
1 2 3 4 5 6 7  def parse(self, response):   #  接收到请求的那个meta参数的值 
引入 crawlspider(正则 Rule) 1 2 3 4 5 6 7 8 #  创建项目 #  创建爬虫文件 #  运行爬虫文件 #  scrapy crawl read  
此时的目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <div  class ="pages" > <span  class ="disabled" > «上一页</span > <span  class ="current" > 1</span > <a  href ="/book/1188_2.html" > 2</a > <a  href ="/book/1188_3.html" > 3</a > <a  href ="/book/1188_4.html" > 4</a > <a  href ="/book/1188_5.html" > 5</a > <a  href ="/book/1188_6.html" > 6</a > <a  href ="/book/1188_7.html" > 7</a > <a  href ="/book/1188_8.html" > 8</a > <a  href ="/book/1188_9.html" > 9</a > <a  href ="/book/1188_10.html" > 10</a > <a  href ="/book/1188_11.html" > 11</a > <a  href ="/book/1188_12.html" > 12</a > <a  href ="/book/1188_13.html" > 13</a > <span > ...</span > <a  class ="disabled"  href ="/book/1188_2.html" > 下一页»</a > </div > 
我们编写如下的代码来匹配这个目录规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #  pipelines.py管道 #  items.py #  settings.py中开启管道 #  Configure item pipelines #  See https://docs.scrapy.org/en/latest/topics/item-pipeline.html #  read.py 
pysql 引入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #  登录mysql #  创建数据库 #  使用数据库 #  创建一个表 #  查看ip 
在 settings.py 引入数据库的信息
1 2 3 4 5 6 7 8 DB_HOST = '192.168.1.100'#  端口号是一个整数 
下载 pymysql
1 pip install pymysql -i https://pypi.douban.com/simple
日志输出 settings.py
1 2 3 4 5 6 #  不常用 #  指定日志级别 #  LOG_LEVEL = "WARNING"  #  常用