本文最后更新于:7 个月前
1概念
2学习目标
Scrapy
框架安装
创建 Scrapy
项目
创建 Scrapy
爬虫
应用运行 Scrapy
掌握Scrapy
定位以及提取方法
掌握Response
响应对象的常用属性
2.1 Scrapy
框架安装 1 pip install -i https:// pypi.doubanio.com/simple/ scrapy
2.2 Scrapy
项目创建 我们先查看一下命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 E:\python学习\myScrapyProject\myScrapyProject>scrapy Scrapy 2.6 .1 - project: myScrapyProject Usage: scrapy <command > [options ] [args ] Available commands: bench Run quick benchmark test check Check spider contracts commands crawl Run a spider edit Edit spider fetch Fetch a URL using the Scrapy downloader genspider Generate new spider using pre-defined templates list List available spiders parse Parse URL (using its spider) and print the results runspider Run a self-contained spider (without creating a project) settings Get settings values shell Interactive scraping console startproject Create new project version Print Scrapy version view Open URL in browser, as seen by Scrapy
1 scrapy startproject myScrapyProject
我们发现下面还有2行文字描述 执行一下(注意,这里myScrapyProject->myScrapyProject )
然后我们用 vsCode
打开项目
3创建 Scrapy
爬虫 scrapy genspider 爬虫名称 url
如下:
1 scrapy genspider cnblogs https://www.cnblogs.com/
这里创建了一个名字叫做 cnblogs
的爬虫, 初始 url
为 https://www.cnblogs.com/
没错,这里是去爬博客园的首页文章~~~
1 2 3 E:\python学习\myScrapyProject\myScrapyProject>scrapy genspider cnblogs https://www.cnblogs.com/ Created spider 'cnblogs' using template 'basic' in module: myScrapyProject.spiders.cnblogs
4应用运行 Scrapy 执行程序
1 scrapy crawl cnblogs --nolog
5掌握Scrapy
定位以及提取方法 我们分析一下博客园,这里强烈推荐一个Xpath 帮助插件
大家可以看到,这段xpath 可以获取文章的所有信息,包括 标题,内容,作者,发布时间等等
我们可以编写我们的 cnblogs
爬虫:
1 2 3 4 5 6 7 8 9 10 11 12 13 import scrapyclass CnblogsSpider (scrapy.Spider): name = 'cnblogs' allowed_domains = ['www.cnblogs.com' ] start_urls = ['http://www.cnblogs.com/' ] def parse (self, response ): print (response.text) nodeXpath=response.xpath("//div[@id='post_list']/article[@class='post-item']" )
我们这里分析一下,
现在我们要获取标题,
1 2 3 4 5 6 7 <article class ="post-item" data-post-id ="16439338" > <section class ="post-item-body" > <div class ="post-item-text" > <a class ="post-item-title xh-highlight" href ="https://www.cnblogs.com/ilyyin/p/16439338.html" target ="_blank" > nifi从入门到实战(保姆级教程)——flow</a > <p class ="post-item-summary" > 本文章首发于博客园,转载请标明出处 经过前两篇文章(环境篇,身份验证),我们已经有了nifi可以运行的基础,今天就来实现一个案例吧。 假设我们要从ftp上获取一个zip包,里面有两个csv文件,一个是manufacture.csv,一个是brand.csv.然后要把这两个文件导入到sqlserver ... </p >
我们前面已经拿到了 //div[@id='post_list']/article[@class='post-item']
然后我们发现,article[@class='post-item']
还有个 section
所以我们的xpath
应该是这样的://section[@class='post-item-body']/div[@class='post-item-text']/a[@class='post-item-title']
,单是我们的 cnblogs
爬虫,我们已经获取到了nodeXpath
了 所以我们这里就要改成 以当前为根节点./section[@class='post-item-body']/div[@class='post-item-text']/a[@class='post-item-title']
(这里//
表示的是以body
为根节点),所以我们的下面代码就可以写成
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 import scrapyclass CnblogsSpider (scrapy.Spider): name = 'cnblogs' allowed_domains = ['www.cnblogs.com' ] start_urls = ['http://www.cnblogs.com/' ] def parse (self, response ): print (response.text) nodeXpath=response.xpath("//div[@id='post_list']/article[@class='post-item']" ) for item in nodeXpath: title=item.xpath("./section/div[@class='post-item-text']/a[@class='post-item-title']/text()" ) summary=item.xpath("./section/div[@class='post-item-text']/p[@class='post-item-summary']/text()" ) summary=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-item-author']/span/text()" ) summary=item.xpath("./section/footer[@class='post-item-foot']/span[@class='post-meta-item']/span/text()" ) summary=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) summary=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) summary=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) print (title)
但是我们这里,发现,这里的值怎么是这样的 [<Selector xpath="./section/div[@class='post-item-text']/a[@class='post-item-title']/text()" data='Unity3D学习笔记4——创建Mesh高级接口'>]
别慌,这里用数据包起来了,我们调整下
1 2 title=item.xpath("./section/div[@class='post-item-text']/a[@class='post-item-title']/text()" )[0 ]
1 scrapy crawl cnblogs --nolog
1 <Selector xpath="./section/div[@class='post-item-text']/a[@class='post-item-title ']/text()" data='Unity3D学习笔记4 ——创建Mesh高级接口'>
6掌握Response 响应对象的常用属性 发现还不是咱们想要的, 这里要介绍两个关键词
extract() extract_first()
extract() 与extract_first()
7最终爬虫效果 7.1代码 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 import scrapyclass CnblogsSpider (scrapy.Spider): name = 'cnblogs' allowed_domains = ['www.cnblogs.com' ] start_urls = ['http://www.cnblogs.com/' ] def parse (self, response ): print (response.text) nodeXpath=response.xpath("//div[@id='post_list']/article[@class='post-item']" ) for item in nodeXpath: article_title=item.xpath("./section/div[@class='post-item-text']/a[@class='post-item-title']/text()" )[0 ].extract() article_title=item.xpath("./section/div[@class='post-item-text']/a[@class='post-item-title']/text()" ).extract_first() article_summary=item.xpath("./section/div[@class='post-item-text']/p[@class='post-item-summary']/text()" ).extract_first() article_autuor=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-item-author']/span/text()" ) .extract_first() article_time=item.xpath("./section/footer[@class='post-item-foot']/span[@class='post-meta-item']/span/text()" ) .extract_first() article_up=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) .extract_first() article_comment=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) .extract_first() article_browse=item.xpath("./section/footer[@class='post-item-foot']/a[@class='post-meta-item btn'][1]/span/text()" ) .extract_first() with open (file="cnblogs.txt" ,mode="a+" ,encoding="utf-8" )as f: f.write(f"{article_title} \t{article_summary} \t{article_autuor} \t{article_time} \t{article_up} \t{article_comment} \t{article_browse} " )
7.2 效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 【计算机系统基础1】gdb、gcc简易使用指南 【计算机系统基础1】gdb、gcc简易使用指南 1. 基本实验工具的使用 1.1GCC 在IA-32+LINUX平台 在IA-32+LINUX平台上,编译,调试和运行C语言 为了把C语言源程序编译成IA-32机器指令,X86-64位计算机系统需要先运行下列命令: sudo apt-get insta ... 阿宇的计算机世界 2022-07-03 16:22 0 0 0Android高仿网易云音乐-启动界面实现和动态权限处理 爱学啊 2022-07-03 16:09 0 0 0前端周刊第二十六期 夕阳白雪 2022-07-03 15:56 0 0 0利用MySQL中的乐观锁和悲观锁实现分布式锁 eaglelihh 2022-07-03 15:13 0 0 0nifi从入门到实战(保姆级教程)——flow 本文章首发于博客园,转载请标明出处 经过前两篇文章(环境篇,身份验证),我们已经有了nifi可以运行的基础,今天就来实现一个案例吧。 假设我们要从ftp上获取一个zip包,里面有两个csv文件,一个是manufacture.csv,一个是brand.csv.然后要把这两个文件导入到sqlserver ... ilyyin 2022-07-03 15:11 0 0 0聊聊Netty那些事儿之从内核角度看IO模型 bin的技术小屋 2022-07-03 14:40 0 0 0docker的平替--podman eryoung2 2022-07-03 13:30 0 0 0抓包整理外篇fiddler———— 会话栏与过滤器[二] 敖毛毛 2022-07-03 10:15 0 0 0rxjava回调地狱-kotlin协程来帮忙 俞正东 2022-07-03 10:11 0 0 0图文看懂JavaScritpt引擎V8与JS执行过程 会飞的一棵树 2022-07-03 00:11 0 0 0测试右移:线上质量监控 ELK 实战 Juno3550 2022-07-02 23:43 0 0 0简单聊聊运维监控的其他用途 charlieroro 2022-07-02 22:55 0 0 0不是吧?30秒 就能学会一个python小技巧?! 小熊猫爱恰饭 2022-07-02 22:54 0 0 0容器运行时分析 JonPan 2022-07-02 22:22 0 0 0程序分析与优化 - 9 附录 XLA的缓冲区指派 周荣华 2022-07-02 21:40 0 0 0分享一下如何制作专业的手绘电子地图 轻轻的烟雾 2022-07-02 21:03 0 0 0ArrayList分析2 :Itr、ListIterator以及SubList中的坑 funnyZpC 2022-07-02 20:50 0 0 0后端接入层技术的一些思考 三国梦回 2022-07-02 20:33 0 0 0技术人创业:失败不是成功,但反思是 freephp 2022-07-02 20:07 3 3 3Unity3D学习笔记4——创建Mesh高级接口 charlee44 2022-07-02 19:32 0 0 0