在线教育网站学习,是很多人利于闲余时间进行充电的一种选择。本篇文章利用python抓取在线教育网站课工场课程页面的数据,进行简要分析。
通过使用requests库对课工场课程列表页进行抓取。
通过BeautifulSoup对课工场课程列表页面进行解析,并从中获课程名称,课程类型,付费类型,课程学习人次的数据。
最后再利用python的pandas,matplotlib,seaborn模块对数据进行处理和分析
0,工作环境搭建
环境:
win10+Anaconda +jupyter Notebook
模块:
爬虫和网页解析相关的模块:
requests,BeautifulSoup,time
数据分析模块:
Numpy,pandas
画图模块:
matplotlib,seaborn
1,构建爬虫,抓取需要的信息
构造爬虫是需要注意
- 开始抓取前先观察下目标页面或网站的结构,其中比较重要的是URL的结构
- 在抓取网页时,为了尽量伪装成正常的请求,我们需要在http请求中设置一个头部信息,否则很容易被封。头部信息网上有很多现成的。并手动设置每次请求的时间间隔。
A,构造爬虫
import requests,time
from bs4 import BeautifulSoup
url = 'http://www.kgc.cn/list/230-'+str(i)+'-6-9-9-0.shtml'
headers={
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'Referer':'http://www.baidu.com/link?url=
_andhfsjjjKRgEWkj7i9cFmYYGsisrnm2A
-TN3XZDQXxvGsM9k9ZZSnikW2Yds4s&
wd=&eqid=c3435a7d00006bd600000003582bfd1f'
}
for i in range(1,64):
if i==1:
url = 'http://www.kgc.cn/list/230-'+str(i)+'-6-9-9-0.shtml'
r = requests.get(url=url,headers=headers)
html = r.text
else:
url = 'http://www.kgc.cn/list/230-'+str(i)+'-6-9-9-0.shtml'
r = requests.get(url=url,headers=headers)
html2 = r.text
html = html + html2
time.sleep(0.8)
B,解析页面,提取信息
kgc = BeautifulSoup(html,"html.parser")
# 提取课程名称
name= kgc.find_all("a",attrs={
"class":"yui3-u course-title-a"})
c_na