即将进入一个知识付费行业,所以想爬取下知乎Live课程的相关数据,做一个简单的数据分析,主要分析目标都列出来了。于是自己写了一个爬虫,但遗憾的是PC端API开放的数据不够全,而且部分课程购买可用优惠券购买,所以课程的收入也不是很准确。
最终获取的数据存储到MySQL中,分别记录了每个课程的id、名称、主讲人、评分、评价数、参与人数、单价、收入等相关数据。
以下为源代码,写的不好,多多见谅
# coding:utf-8
# 分析目标
# 1、课程总收入=单价*学习人数
# 2、每天产生的课程数,形成一个趋势图(日期*课程数)
# 3、参与人数最多课程排序
# 4、收入最多课程排序
# 5、获得所有课程的tag,以及对应tag的课程数量,按课程数量对tag进行排序
# 6、每个课程的相关数据:标题、主讲人、评分、评价数、语音条数、问答数、文件数、参与数、单价
# 7、主讲人和他的所有课程,以及课程数量
# 8、主讲人获得的总收入,每个课程的收入
# 涉及到的URL:
# 知乎Live列表:https://api.zhihu.com/lives/homefeed?includes=live
# 知乎Live详情:https://api.zhihu.com/lives/868793703320408064
import requests
import json
import MySQLdb
# 定义get_course_url,获取课程详情页的url
def get_course_url():
url = 'https://api.zhihu.com/lives/homefeed?includes=live&limit=88'
headers = {
'referer': 'https://www.zhihu.com/lives',
'host': 'api.zhihu.com',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
response = requests.get(url, headers=headers)
res = response.content
resDict = json.loads(res) # 把json数据转化为dict数据类型
courseDict = resDict['data'] # 获取数据里的课程数据,存储在key为data的数据里
courseUrlList = []
for i in range(len(courseDict)):
courseId = courseDict[i]['live']['id'] # 获取课程id,id数据放在key为live里的数据里
firstUrl = 'https://api.zhihu.com/lives/' # 知乎live详情页的url前缀
url = firstUrl + str(courseId) # 组成一个完整的知乎live详情页url
courseUrlList.append(url)
print '获取详情页URL成功'
return courseUrlList # 返回一个由详情页url组成的列表
# 定义获取课程信息的方法,并且传入一个urlList,这是需要爬取的url集合
def get_course_info(urlList):
courseList = []
for url in urlList:
headers = {
'referer': 'https://www.zhihu.com/lives',
'host': 'api.zhihu.com',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
response = requests.get(url, headers=headers)
res = response.content
resDict = json.loads(res)
courseDict = {
'id': resDict['id'],
'name': resDict['subject'],
'speakName': resDict['speaker']['member']['name'],
'rank': resDict['feedback_score'],
'commentNum': resDict['review']['count'],
'answerNum': resDict['reply_message_count'],
'seatNum': resDict['seats']['taken'],
'fee': resDict['fee']['amount'],
'income': float(resDict['seats']['taken']) * (float(resDict['fee']['amount']) / 100)
}
courseList.append(courseDict)
print '获取课程列表成功'
return courseList
# 定义inser_data方法,存储获取到的数据
def insert_data(list):
db = MySQLdb.connect(host='localhost', user='root', passwd='mark2227_', db='zhihu', charset='utf8')
cursor = db.cursor()
print '连接数据库成功'
try:
createSql = '''create table liveCourse(
id bigint not null,
name varchar(25) not null,
speakName varchar(25) not null,
rank float,
commentNum int,
answerNum int,
seatNum int,
fee int,
income float
)
'''
cursor.execute(createSql)
print '成功创建表'
db.commit()
for course in list:
param = []
param.append(int(course['id']))
param.append(course['name'])
param.append(course['speakName'])
param.append(float(course['rank']))
param.append(int(course['commentNum']))
param.append(int(course['answerNum']))
param.append(int(course['seatNum']))
param.append(int(course['fee']))
param.append(float(course['income']))
insertSql = 'insert into liveCourse(id,name,speakName,rank,commentNum,answerNum,seatNum,fee,income) Values(%s,%s,%s,%s,%s,%s,%s,%s,%s)'
cursor.execute(insertSql, param)
db.commit()
print '成功插入一条数据'
except:
print '插入数据失败'
db.rollback()
db.close()
if __name__ == '__main__':
urlList = get_course_url()
courseList = get_course_info(urlList)
insert_data(courseList)
网友评论