前两篇文章中,简单用scrapy写了一个小demo,本篇文章主要目标是完整用scrapy爬取,慕课网所有免费的课程、标题、图片、地址、学习人数、难度、方向、分类、时长、评分、评论数等。
其实,很简单,在上一次的代码中修改调整一下就好。
# -*- coding: utf-8 -*-
import scrapy
import json
from urllib import parse as urlparse
from scrapyDemo.ImoocCourseItem import ImoocCourseItem
# 慕课网爬取
class ImoocSpider(scrapy.Spider):
# spider的名字定义了Scrapy如何定位(并初始化)spider,所以其必须是唯一的
name = "imooc"
# URL列表
start_urls = ['http://www.imooc.com/course/list']
# 域名不在列表中的URL不会被爬取。
allowed_domains = ['www.imooc.com']
def parse(self, response):
# 课程类型
types = response.css('div.course-content .course-nav-row')[2].css(
'.bd ul li a')
for key in range(len(types)):
if key == 0:
continue
course_type = types[key].css('::text').extract_first()
# 类型的url
type_url = types[key].css('::attr(href)').extract_first()
# print (item)
yield scrapy.Request(
url=urlparse.urljoin(response.url, type_url),
callback=self.parse_by_type,
meta={
'course_type': course_type
})
# 按课程类型爬取
def parse_by_type(self, response):
itemBase = response.meta
item = ImoocCourseItem()
item['course_type'] = itemBase['course_type']
# print(item)
learn_nodes = response.css('a.course-card')
# 遍历该页上所有课程列表
for learn_node in learn_nodes:
course_url = learn_node.css("::attr(href)").extract_first()
# 拼接课程详情页地址
course_url = urlparse.urljoin(response.url, course_url)
# 课程地址
item['course_url'] = course_url
# 课程图片
item['image'] = learn_node.css(
"img.course-banner::attr(src)").extract_first()
# 分类
cate = learn_node.css("div.course-label label::text").extract()
item['cate'] = ','.join(cate)
# 进入课程详情页面
yield scrapy.Request(
url=course_url, callback=self.parse_learn, meta=item)
# 下一页地址
next_page_url = response.css(
u'div.page a:contains("下一页")::attr(href)').extract_first()
if next_page_url:
yield scrapy.Request(
url=urlparse.urljoin(response.url, next_page_url),
callback=self.parse_by_type,
meta={
'course_type': item['course_type']
})
# 课程详情
def parse_learn(self, response):
item = response.meta
# 课程标题
item['title'] = response.xpath(
'//h2[@class="l"]/text()').extract_first()
# 课程简介
item['brief'] = response.xpath(
'//div[@class="course-brief"]/p/text()').extract_first()
staticItem = response.css(
'div#main div.statics div.static-item span.meta-value::text'
).extract()
# 难度级别
item['difficulty_level'] = staticItem[0]
# 课程时长
item['duration'] = staticItem[1]
# 综合评分
item['overall_rating'] = staticItem[2]
# 评论数
item['evaluation_number'] = response.css(
'a.person-num span.person-num::text').extract_first().replace(
'人评价', '')
# 教师id
item['teacher_id'] = response.css(
'div.teacher-info a img::attr(data-userid)').extract_first()
# 学习人数
ids = response.url.split('/')[-1]
yield scrapy.Request(
url=urlparse.urljoin(response.url,
'/course/AjaxCourseMembers?ids=' + ids),
callback=self.parse_learn_num,
meta=item)
# 爬取学习人数
def parse_learn_num(self, response):
item = response.meta
data = json.loads(response.body_as_unicode())
# 学习人数
item['learn_num'] = data['data'][0]['numbers']
# print (item)
yield item