| |
| -登录cnblogs,拿到cookie,再打开cnblogs,写入cookie,它就是登录状态 |
| |
| -半自动点赞---》selenium生成的cookie,给requests用 |
| -selenium操作浏览器,速度慢 |
| -requests速度快 |
| |
| -动作链 |
| -自动登录12306 |
| |
| -帮我们破解验证码---》超级鹰,云打码 |
| -验证码图片,发送给破解平台,把结果给你,你输入进去 |
| -获取到验证码图片 |
| -1 截图 |
| -2 如果图片用base64编码的,直接保存到本地即可 |
| |
| |
| -使用打码平台,登录打码平台 |
| |
| |
| |
| - / |
| - // |
| -. |
| -.. |
| -标签名字 |
| -@属性名 |
| |
| |
| |
| |
0 scrapy架构介绍
| |
| 引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。 |
| |
| |
| 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址 |
| |
| |
| 用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的 |
| |
| |
| SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求 |
| |
| |
| 在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作 |
| |
| |
| |
| 位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,你可用该中间件做以下几件事:设置请求头,设置cookie,使用代理,集成selenium |
| |
| |
| 位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests) |
0.1 scrapy的一些命令
| |
| |
| |
| scrapy startproject 项目名字 |
| |
| |
| |
| scrapy genspider 名字 域名 |
| |
| |
| |
| |
| |
| scrapy crawl 爬虫名字 |
| |
| |
| 新建一个run.py,写入,以后右键执行即可 |
| from scrapy.cmdline import execute |
| execute(['scrapy','crawl','cnblogs','--nolog']) |
| |
| |
| |
| |
| http://www.cnblogs.com/robots.txt |
0.2 scrapy项目目录结构
| firstscrapy |
| firstscrapy |
| spiders |
| __init__.py |
| baidu.py |
| cnblogs.py |
| items.py |
| middlewares.py |
| pipelines.py |
| run.py |
| settings.py |
| scrapy.cfg |
1 scrapy解析数据
| 1 response对象有css方法和xpath方法 |
| -css中写css选择器 |
| -xpath中写xpath选择 |
| 2 重点1: |
| -xpath取文本内容 |
| './/a[contains(@class,"link-title")]/text()' |
| -xpath取属性 |
| './/a[contains(@class,"link-title")]/@href' |
| -css取文本 |
| 'a.link-title::text' |
| -css取属性 |
| 'img.image-scale::attr(src)' |
| 3 重点2: |
| .extract_first() 取一个 |
| .extract() 取所有 |
| |
解析cnblosg
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| def parse(self, response): |
| |
| article_list = response.xpath('//*[@id="post_list"]/article') |
| |
| for article in article_list: |
| title = article.xpath('.//div/a/text()').extract_first() |
| author_img = article.xpath('.//div//img/@src').extract_first() |
| author_name = article.xpath('.//footer//span/text()').extract_first() |
| desc_old = article.xpath('.//p/text()').extract() |
| desc = desc_old[0].replace('\n', '').replace(' ', '') |
| if not desc: |
| desc = desc_old[1].replace('\n', '').replace(' ', '') |
| url = article.xpath('.//div/a/@href').extract_first() |
| |
| print(title) |
| print(author_img) |
| print(author_name) |
| print(desc) |
| print(url) |
2 settings相关配置,提高爬取效率
2.1 基础的一些
| |
| BOT_NAME = "firstscrapy" |
| |
| SPIDER_MODULES = ["firstscrapy.spiders"] |
| NEWSPIDER_MODULE = "firstscrapy.spiders" |
| |
| |
| ROBOTSTXT_OBEY = False |
| |
| USER_AGENT = "firstscrapy (+http://www.yourdomain.com)" |
| |
| LOG_LEVEL='ERROR' |
| |
| |
| DEFAULT_REQUEST_HEADERS = { |
| 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
| 'Accept-Language': 'en', |
| } |
| |
| |
| SPIDER_MIDDLEWARES = { |
| 'cnblogs.middlewares.CnblogsSpiderMiddleware': 543, |
| } |
| |
| DOWNLOADER_MIDDLEWARES = { |
| 'cnblogs.middlewares.CnblogsDownloaderMiddleware': 543, |
| } |
| |
| |
| ITEM_PIPELINES = { |
| 'cnblogs.pipelines.CnblogsPipeline': 300, |
| } |
2.2 增加爬虫的爬取效率
| |
| 默认scrapy开启的并发线程为32个,可以适当进行增加。在settings配置文件中修改 |
| CONCURRENT_REQUESTS = 100 |
| 值为100,并发设置成了为100。 |
| |
| 在运行scrapy时,会有大量日志信息的输出,为了减少CPU的使用率。可以设置log输出信息为INFO或者ERROR即可。在配置文件中编写: |
| LOG_LEVEL = 'INFO' |
| |
| 如果不是真的需要cookie,则在scrapy爬取数据时可以禁止cookie从而减少CPU的使用率,提升爬取效率。在配置文件中编写: |
| COOKIES_ENABLED = False |
| |
| 对失败的HTTP进行重新请求(重试)会减慢爬取速度,因此可以禁止重试。在配置文件中编写: |
| RETRY_ENABLED = False |
| |
| 如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速被放弃,从而提升效率。在配置文件中进行编写: |
| DOWNLOAD_TIMEOUT = 10 超时时间为10s |
3 持久化方案
| |
| scrapy crawl cnblogs -o cnbogs.json |
| |
| |
| -第一步:在item.py中写一个类 |
| class FirstscrapyItem(scrapy.Item): |
| title = scrapy.Field() |
| author_img = scrapy.Field() |
| author_name = scrapy.Field() |
| desc = scrapy.Field() |
| url = scrapy.Field() |
| |
| content = scrapy.Field() |
| |
| -第二步:在pipline.py中写代码,写一个类:open_spide,close_spider,process_item |
| -open_spide:开启爬虫会触发 |
| -close_spider:爬完会触发 |
| -process_ite:每次要保存一个对象会触发 |
| class FirstscrapyFilePipeline: |
| def open_spider(self, spider): |
| print('我开了') |
| self.f=open('a.txt','w',encoding='utf-8') |
| def close_spider(self, spider): |
| print('我关了') |
| self.f.close() |
| |
| def process_item(self, item, spider): |
| self.f.write(item['title']+'\n') |
| return item |
| -第三步:配置文件配置 |
| ITEM_PIPELINES = { |
| "firstscrapy.pipelines.FirstscrapyFilePipeline": 300, |
| } |
| |
| -第四步:在解析方法parse中yield item对象 |
| |
| |
4 全站爬取cnblogs文章
4.1 request和response对象传递参数
| |
| yield Request(url=url, callback=self.detail_parse,meta={'item':item}) |
| |
| |
| yield item |
4.2 解析下一页并继续爬取
| import scrapy |
| from bs4 import BeautifulSoup |
| from firstscrapy.items import FirstscrapyItem |
| from scrapy.http.request import Request |
| |
| class CnblogsSpider(scrapy.Spider): |
| name = "cnblogs" |
| allowed_domains = ["www.cnblogs.com"] |
| start_urls = ["http://www.cnblogs.com/"] |
| |
| |
| def parse(self, response): |
| article_list = response.xpath('//*[@id="post_list"]/article') |
| |
| |
| for article in article_list: |
| item = FirstscrapyItem() |
| title = article.xpath('.//div/a/text()').extract_first() |
| item['title'] = title |
| author_img = article.xpath('.//div//img/@src').extract_first() |
| item['author_img'] = author_img |
| author_name = article.xpath('.//footer//span/text()').extract_first() |
| item['author_name'] = author_name |
| desc_old = article.xpath('.//p/text()').extract() |
| desc = desc_old[0].replace('\n', '').replace(' ', '') |
| if not desc: |
| desc = desc_old[1].replace('\n', '').replace(' ', '') |
| item['desc'] = desc |
| url = article.xpath('.//div/a/@href').extract_first() |
| item['url'] = url |
| |
| |
| |
| yield Request(url=url,callback=self.parser_detail,meta={'item':item}) |
| |
| |
| next='https://www.cnblogs.com'+response.css('div.pager>a:last-child::attr(href)').extract_first() |
| print(next) |
| yield Request(url=next,callback=self.parse) |
| |
| |
| |
| |
| def parser_detail(self,response): |
| |
| |
| content=response.css('#cnblogs_post_body').extract_first() |
| |
| |
| item=response.meta.get('item') |
| if content: |
| item['content']=content |
| else: |
| item['content'] = '没查到' |
| yield item |
| |
| |
| |
5 爬虫和下载中间件
6.1 加代理
6.2 加cookie,修改请求头,随机生成UserAgent
6.3 集成selenium
| |
| - 编码水平 |
| - 公司的看不了,给他看的,全是个人项目 |
| - 公司项目看不了,签了保密协议 |
| |
| |
| |
| -云数据库:阿里云数据库,花钱,买服务---》账号和密码---》公司不需要自己搭建mysql |
| -mysql |
| -redis |
| -mongodb |
| -自己的数据库,部署在云服务器上的数据库,是你自己的 |
| |
| |
| |
| - 阿里云的ecs,服务器 |
| - 阿里云的oss ,对象存储 |
| - 云短信 |
| - 七牛云 文件存储 |
| |
| |
| |
| -配置文件 dev.py 连的是本地127.0.0.1 |
| -你们上线怎么弄 |
| -我不知道,就是给我一个地址,端口,用户名密码 |
| |
| - 上线的数据库服务和项目服务 是在同一台及其上吗? |
| |
| |
| 定时任务 |
| 定时 |
| 异步任务 |
| |
| |
| |
| |
| -用过 |
| -怎么用的?两种方式 |
| -类实例化得到对象Thread类 传入target 任务函数,对象.start |
| -写一个类,继承Thread,重写run方法,写任务 类实例化得到对象 对象.start |
| -如果io密集型,用多线程,计算密集型用多进程 ---》只针对于cpython |
| |
| |
| |
| 唯一索引和联合索引有什么区别 |
| |
| |
| |
| -因为它就两种状态,建立索引是没用的,即便建立索引,也不会走 |
| |
| |
| |
| |
| |
| -session机制---》表中把它那条记录删除 |
| -token机制----》 |
| -下线人id----》放个位置 |
| -进入到认证类中---》 |
| |
| |
| -建立个黑名单表 |
| -id 用户id ,ip,时间。。。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架