前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >scrapy 爬虫框架学习-1

scrapy 爬虫框架学习-1

作者头像
kirin
发布2020-05-09 15:54:01
4880
发布2020-05-09 15:54:01
举报
文章被收录于专栏:Kirin博客

使用scrapy 首先安装 pip install scrapy

接着创建项目 scrapy startproject test 爬虫名字 项目创建好了之后创建爬虫app scrapy genspider appname 666cc.cn 第一个是app名字,第二个是需要爬的域名,后面可以更改 *** 接着来写一个爬取小例子 *** class TestSpider(scrapy.Spider): name = ‘test’ allowed_domains = [‘itcast.cn’] start_urls = [‘http://www.itcast.cn/channel/teacher.shtml’]

def parse(self, response): a=response.xpath(‘//div[@class=”tea_con”]//h3/text()’).extract() li_list=response.xpath(‘//div[@class=”tea_con”]//li’) for li in li_list: item={} item[‘name’]=li.xpath(‘.//h3/text()’).extract_first() item[‘title’]=li.xpath(‘.//h4/text()’).extract_first() yield item 通过生成器生成item给管道进行数据处理 这是爬取itcast.cn的例子,获取所有的老师信息 *** 接着来到管道处理,也就是数据处理的地方 *** class Test1Pipeline(object): def process_item(self, item, spider): item[‘hello’]=’hello’ return item

class Test1Pipeline1(object): def process_item(self, item, spider): print(item) with open(‘./test.txt’,’a+’,encoding=’utf-8′)as f: f.write(item[‘name’]) f.write(item[‘title’]) f.write(‘\n’) return item 这里保存爬取的数据到本地 *** setting设置项 *** LOG_LEVEL=’WARNING’ 警告以上级别才显示,保持终端清爽 *** 设置管道优先级,数字越小,优先级越高 ITEM_PIPELINES = { ‘test1.pipelines.Test1Pipeline’: 300, # 设置优先级 ‘test1.pipelines.Test1Pipeline1’: 301, # 设置优先级 } ***

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/04/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档