python scrapy库_Python爬虫学习(三)使用Scrapy库

(一)Scrapy库概述

1,安装:pip install scrapy失败;

运行D:\Python\Python36\python.exe -m pip install –upgrade pip命令升级pip命令失败;

安装:D:\Python\Python36\python.exe -m pip install wheel

安装:D:\Python\Python36\python.exe -m pip install scrapy

2,框架概述:

python scrapy库_Python爬虫学习(三)使用Scrapy库

python scrapy库_Python爬虫学习(三)使用Scrapy库

入口:SPIDERS;出口:ITEM PIPELINES;用户编写SPIDERS(URL),ITEM PIPELINES(对数据处理)

ENGINE:控制所有模块之间的数据流,根据条件触发事件,不允许用户修改,代码已实现

DOWNLOADER:根据请求下载网页,功能单一,不允许用户修改,代码已实现

SCHEDULER:对所有爬取请求进行调度管理,不允许用户修改,代码已实现

DOWNLOADER MIDDLEWARE:用户可以修改配置;中间件

python scrapy库_Python爬虫学习(三)使用Scrapy库

SPIDERS:解析DOWNLOADER返回的响应(Response);产生爬取项(scraped item);产生额外的爬取请求(Request);需要用户编写的最核心代码

ITEM PIPELINES:以流水线方式处理Spider产生的爬取项;由一组操作顺序组成,每个操作是一个item pipelines类型;操作可包括:清理,检验,查重爬虫项中HTML数据

将数据存入数据库;由用户编写功能

SPIDER MIDDLEWARE:中间件

python scrapy库_Python爬虫学习(三)使用Scrapy库

3,requests库和Scrapy库区别:

python scrapy库_Python爬虫学习(三)使用Scrapy库

python scrapy库_Python爬虫学习(三)使用Scrapy库

python scrapy库_Python爬虫学习(三)使用Scrapy库

(二)Scrapy库的使用

1,Scrapy命令行:常用命令:创建过程,创建爬虫,运行爬虫为最常用命令

python scrapy库_Python爬虫学习(三)使用Scrapy库

2,爬取某个HTML:

(1)建立过程: scrapy startproject python123demo

python scrapy库_Python爬虫学习(三)使用Scrapy库

(2)建立爬虫demo:scrapy genspider demo python123.io;建立demo.py文件

demo.py#-– coding: utf-8 –

importscrapyclassDemoSpider(scrapy.Spider):

name= ‘demo’allowed_domains= [‘python123.io’] #说明只能爬取这个文件下的URL

start_urls = [‘http://python123.io/’]def parse(self, response): #处理响应,解析内容形成字典,发现新的URL爬取请求

pass

(3),修改爬虫文件deom.py文件

– coding: utf-8 –

importscrapyclassDemoSpider(scrapy.Spider):

name= ‘demo’

allowed_domains = [‘python123.io’] #说明只能爬取这个文件下的URL

start_urls = [‘http://python123.io/ws/demo.html’]def parse(self, response): #处理响应,解析内容形成字典,发现新的URL爬取请求

fname=response.url.split(“/”)[-1]

with open(fname,”wb”) as f:

f.write(response.body)

self.log(“保存文件:%s”%name)

(4),运行爬虫:scrapy crawl demo

2,yield:

python scrapy库_Python爬虫学习(三)使用Scrapy库

例子:

python scrapy库_Python爬虫学习(三)使用Scrapy库

python scrapy库_Python爬虫学习(三)使用Scrapy库

3,相关类:

(1)request类:

python scrapy库_Python爬虫学习(三)使用Scrapy库

(2)response类:

python scrapy库_Python爬虫学习(三)使用Scrapy库

(3)Item类:类字典类型,可以按照字典类型操作;表示从HTML中提取的内容

4,CSS Selector:

python scrapy库_Python爬虫学习(三)使用Scrapy库

python scrapy库_Python爬虫学习(三)使用Scrapy库

(三)实例:

实例一:

1,功能:

python scrapy库_Python爬虫学习(三)使用Scrapy库

2,实现难点:

3,准备工作:网站选取原则:

python scrapy库_Python爬虫学习(三)使用Scrapy库

爬取链接:http://quote.eastmoney.com/stock_list.html#sh 东方财富股票综合排名

http://gu.qq.com/xxxxxx/gp 腾讯个股股票信息

先爬取股票综合排名:获取股票代码,放入搜狐股票的链接中转到个股信息

爬取股票综合排名网页发现:股票简略信息都在

中;一个行内是一支股票的信息;一行的第一列是代码,第二列 是股票名称

4,步骤:

原文:https://www.cnblogs.com/lq13035130506/p/12254076.html

Original: https://blog.csdn.net/weixin_34274601/article/details/114912412
Author: 天眞無鞋
Title: python scrapy库_Python爬虫学习(三)使用Scrapy库

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/791614/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球