1,pip startproject internInfo  新建一个Scrapy工程
在F:\scrapy路径,执行pip startproject internInfo后,
通过tree internInfo /f查看,生成如图所示文件结构(也可以通过tree internInfo /f > tree.txt,将输出信息写入指定文件夹中)
 scrapy初识
熟记于心:
  • scrapy.cfg: 全局配置文件
  • internInfo/: 项目python模块
  • internInfo/items.py: 项目items文件,定义爬取的数据保存结构
  • internInfo/pipelines.py: 项目管道文件,对爬取来的数据进行清洗、筛选、保存等操作
  • internInfo/settings.py: 项目配置文件
  • internInfo/spiders: 放置spider的目录
2,编写items.py文件
 scrapy初识
3,编写爬虫
可以通过scrapy genspider SMSpider douban.com 实现
 scrapy初识
SMSpider.py内容
 scrapy初识
(1)官网解释:
 scrapy初识
allowed_domains中包括的域名或者是子域名可以正常爬取。
(2)很明显命令生成的start_urls的路径格式不对,应该是https协议,且没有添加www。
我直接修改有效链接
 scrapy初识
4,执行爬虫
进入项目的根目录,执行下列命令启动爬虫:scrapy crawl SMSpider
(1)状态码403 forbidden
scrapy初识
 解决方法:
工程下internInfo下的internInfo文件下的settings.py文件中按模板指示,在相应的地方添加
USER_AGENT='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
再次启动scrapy crawl SMSpider,成功。
 scrapy初识
在根目录下生成4920389文件存储网页body
scrapy初识
控制台输出日志:

scrapy初识

 scrapy初识

对应scrapy官方给出数据流图:

scrapy初识

1. The Engine gets the initial Requests to crawl from the Spider.
2. The Engine schedules the Requests in the Scheduler and asks for the next Requests to crawl.
3. The Scheduler returns the next Requests to the Engine.
4. The Engine sends the Requests to the Downloader, passing through the Downloader Middlewares (see process_request()).
5. Once the page finishes downloading the Downloader generates a Response (with that page) and sends it to the Engine, passing through the Downloader Middlewares (see process_response()).
6. The Engine receives the Response from the Downloader and sends it to the Spider for processing, passing through the Spider Middleware (see process_spider_input()).
7. The Spider processes the Response and returns scraped items and new Requests (to follow) to the Engine, passing through the Spider Middleware (see process_spider_output()).
8. The Engine sends processed items to Item Pipelines, then send processed Requests to the Scheduler and asks for possible next Requests to crawl.
9. The process repeats (from step 1) until there are no more requests from the Scheduler.

附:

问题1:
IndentationError:unexpected indent
python代码的格式未对齐
问题2:
ImportError:No module named win32api
Python是没有自带访问windows系统API的库的,需要另外下载。库的名称叫pywin32,可以从网上直接下载。

执行pip install pypiwin32


参考:

http://python.jobbole.com/86651/ scrapy示例

https://github.com/marchtea/scrapy_doc_chs/blob/master/intro/tutorial.rst#id1 翻译文档

https://docs.scrapy.org/en/latest/topics/architecture.html 官方英文文档

https://www.cnblogs.com/zhaijiahui/p/6984667.html

相关文章:

  • 2021-08-22
  • 2021-07-21
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-10-29
  • 2021-12-27
猜你喜欢
  • 2021-05-08
  • 2021-08-11
  • 2022-12-23
  • 2021-06-28
  • 2021-11-23
相关资源
相似解决方案