scrapy知识积累

Scrapy 中文文档
https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/overview.html

创建项目 scrapy startproject ****(项目名) 创建一个基础爬虫类 scrapy genspider ****spiders名) "–---"(爬虫作用范围) 例:scrapy genspider meiju meijutt.com scrapy genspider -t 模板名字 爬虫名字 爬虫的网址
执行命令,运行程序 scrapy crawl ****(爬虫名).
用于调试
scrapy shell
爬虫的网址
例:scrapy shell meijutt.com
 
items.py 负责数据模型的建立,类似于实体类。存放的是我们要爬取数据的字段信息
middlewares.py 自己定义的中间件。
pipelines.py 负责对spider返回数据的处理。可以让写入到数据库,也可以让写入到文件等等。
settings.py 负责对整个爬虫的配置。
spiders目录 负责存放继承自scrapy的爬虫类。为主要的爬虫代码,包括了对页面的请求以及页面的处理
scrapy.cfg scrapy基础配置


Scrapy爬虫入门教程十三 Settings(设置)

 

 

 通过python代码运行spider项目:

在scrapy.cfg同一目录下创建python文件

 

from scrapy import cmdline
cmdline.execute("scrapy crawl 爬虫名".split())

 

 

 

通过python代码启动spider程序:
 1 from twisted.internet import reactor, defer
 2 from scrapy.crawler import CrawlerProcess
 3 from scrapy.utils.project import get_project_settings
 4 
 5 from xx.xx.spiders.xx import xxSpider
 6 
 7 spider = xxSpider()
 8 settings = get_project_settings()
 9 
10 crawler = CrawlerProcess(settings)
11 
12 dfs = set()
13 d = crawler.crawl(xxSpider)
14 
15 dfs.add(d)
16 defer.DeferredList(dfs).addBoth(lambda _: reactor.stop())
17 reactor.run()

http code http://www.runoob.com/http/http-status-codes.html
https://www.cnblogs.com/yezuhui/p/6850535.html
  1** 信息,服务器收到请求,需要请求者继续执行操作
  2** 成功,操作被成功接收并处理
  3** 重定向,需要进一步的操作以完成请求
  4** 客户端错误,请求包含语法错误或无法完成请求
    400代表客户端发送的请求有语法错误,401代表访问的页面没有授权,403表示没有权限访问这个页面,404代表没有这个页面
  5** 服务器错误,服务器在处理请求的过程中发生了错误
 
 
原文地址:https://www.cnblogs.com/cekong/p/9968733.html