爬虫实践---Scrapy-爬取慕课网热门课程
来源:互联网 发布:淘宝店铺换身份证 编辑:程序博客网 时间:2024/06/05 06:44
访问的网站是:http://www.imooc.com/course/list?sort=pop
首先我们创建一个Scrapy项目
$ scrapy startproject mooc_subjectsNew Scrapy project 'mooc_subjects', using template directory '/home/pit-yk/anaconda3/lib/python3.6/site-packages/scrapy/templates/project', created in: /media/pit-yk/办公/python/codes/知乎专栏---Ehco/Scrapy/mooc_subjectsYou can start your first spider with: cd mooc_subjects scrapy genspider example example.com
$ tree.├── mooc_subjects│ ├── __init__.py│ ├── items.py│ ├── middlewares.py│ ├── pipelines.py│ ├── __pycache__│ │ ├── __init__.cpython-36.pyc│ │ ├── items.cpython-36.pyc│ │ ├── pipelines.cpython-36.pyc│ │ └── settings.cpython-36.pyc│ ├── settings.py│ └── spiders│ ├── __init__.py│ ├── MySpider.py│ └── __pycache__│ ├── __init__.cpython-36.pyc│ └── MySpider.cpython-36.pyc├── mooc_subjects.txt└── scrapy.cfg4 directories, 15 files
这是我程序运行结束后的tree,我在开始构造这个框架的时候:
第一步,先浏览网页html,查看能扣取到的信息有哪些?
以上每个<div class="course-card-container">就是一门课程框,接下来进入每一门课程查看详细信息:
然后就知道可以得到的信息有:
课程标题:title
课程视频链接:url
课程图片:image_url
课程简介:introduction
课程学习总人数:student
那就构造出了---items.py
$ cat items.py # -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass MoocSubjectsItem(scrapy.Item): # define the fields for your item here like: #name = scrapy.Field() # 课程标题 title = scrapy.Field() #课程url url = scrapy.Field() #课程标题图片 image_url = scrapy.Field() #课程描述 introduction = scrapy.Field() #学习人数 student = scrapy.Field()
紧接着,根据HTML框架得到---MySpider.py
$ cat MySpider.py #!/usr/bin/env python# coding=utf-8import scrapyfrom mooc_subjects.items import MoocSubjectsItemclass MySpider(scrapy.Spider): name = "MySpider" # 设定域名 allowed_domains = ['imooc.com'] # 填写爬取地址 start_urls = ["http://www.imooc.com/course/list"] # 填写爬取方法 def parse(self, response): # 实例一个容器保存爬取信息 item = MoocSubjectsItem() for box in response.xpath('//div[@class="course-card-container"]'): item['url'] = 'http://www.imooc.com' + box.xpath('.//@href').extract()[0] item['image_url'] = box.xpath('.//@src').extract()[0] item['title'] = box.xpath('.//h3[@class="course-card-name"]/text()').extract()[0] item['introduction'] = box.xpath('.//p/text()').extract()[0] item['student'] = box.xpath('.//div[@class="course-card-info"]/span[2]/text()').extract()[0] yield item # 返回信息
然后,顺理成章的得到---pipelines.py
$ cat pipelines.py # -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.htmlimport osclass MoocSubjectsPipeline(object): def process_item(self, item, spider): # 获取当前工作目录 base_dir = os.getcwd() # 文件存储位置 fiename = base_dir + '/mooc_subjects.txt' # 以内存追加的方式打开文件并写入对应的数据 with open(fiename,'a') as f: f.write(item['title'] + '\t') f.write(item['student'] + '人学习'+'\n') f.write(item['introduction'] + '\n') f.write(item['url'] + '\n\n') # 下载图片 return item
下载图片这个语句我感觉没有什么用,暂时用不到这个东西,就没有加上去。
最后,但又是最重要的---配置设置文件,根据自己需求,目前这个框架感觉只需要
ITEM_PIPELINES = { 'mooc_subjects.pipelines.MoocSubjectsPipeline': 300,}
把它弄回来就好。
然后...没有然后了,就得到了
但是后来感觉少些东西,貌似只爬取了网页第一页,如果我要爬取更多的页面呢?
方法1:
#在MySpider.py里面将原来的 start_urls = []替换为下面的,我们就可以爬取指定页 start_urls = [] # 通过简单的循环,来生成爬取页面的列表 # 这里我们爬1~5页 for i in range(1, 6): start_urls.append('http://www.imooc.com/course/list?page=' + str(i))
方法2:
def parse(self, response): ...... #url跟进开始 #获取下一页的url信息 url = response.xpath("//a[contains(text(),'下一页')]/@href").extract() if url : #将信息组合成下一页的url page = 'http://www.imooc.com' + url[0] #返回url yield scrapy.Request(page, callback=self.parse) #url跟进结束
方法二是知道没有下一页为止,方法一时指定页面,根据不同的需求来用不同的方法。
阅读全文
0 0
- 爬虫实践---Scrapy-爬取慕课网热门课程
- scrapy爬虫-爬取慕课网全部课程
- Python爬虫Scrapy实践
- Scrapy爬虫原理及实践
- 爬虫实践之爬虫框架Scrapy安装
- 爬虫scrapy抓取w3school课程列表
- 爬虫实践(四)--scrapy简单实践
- Scrapy爬虫+Tkinter爬取天猫热门商品(一)
- Scrapy爬虫+Tkinter爬取天猫热门商品(二)
- Scrapy爬虫+Tkinter爬取天猫热门商品(三)
- 爬虫实践(三)--了解scrapy
- Python爬虫-爬取慕课网课程
- scrapy爬虫
- Scrapy 爬虫
- scrapy爬虫
- 爬虫-scrapy
- windows 7 Scrapy爬虫安装成功实践记录
- 爬虫实践---Scrapy-豆瓣电影影评&深度爬取
- 抗不过,还是实名认证了下。。。
- 数值孔径大小对微透镜匀光的影响
- js正则表达式
- 对MVC、MVVM开发模式的理解和总结
- 为什么使用MQ
- 爬虫实践---Scrapy-爬取慕课网热门课程
- SVM入门(五)线性分类器的求解——问题的描述Part2
- hpuoj【1389】梦想从A+B开始【水题】&&【转义字符的输出】
- Java中如何遍历Map对象的4种方法
- poj 2063 Investment(完全背包)
- Android Binder 修炼之道(四)Binder 系统C++ 发送数据过程以及Server注册服务处理数据的过程
- HDU 4812 D Tree (2013-南京 树分治 + 逆元 + 离散化)
- jdk源码分析--Enum
- Android判断网线连接