-
Notifications
You must be signed in to change notification settings - Fork 0
/
jikexueyuan1.py
75 lines (62 loc) · 2.86 KB
/
jikexueyuan1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*-coding:utf8-*-
import requests
import re
import sys
reload(sys)
class spider(object):
def __init__(self):
print u'开始爬取内容。。。'
# getsource用来获取网页源代码
def getsource(self, url):
html = requests.get(url)
return html.text
# changepage用来生产不同页数的链接
def changepage(self, url, total_page):
now_page = int(re.search('pageNum=(\d+)', url, re.S).group(1))
page_group = []
for i in range(now_page, total_page + 1):
link = re.sub('pageNum=\d+', 'pageNum=%s' % i, url, re.S)
page_group.append(link)
return page_group
# geteveryclass用来抓取每个课程块的信息
def geteveryclass(self, source):
everyclass = re.findall('(<li id="\d+" test="\d+" deg="0".*?</li>)', source, re.S)
return everyclass
# getinfo用来从每个课程块中提取出我们需要的信息
def getinfo(self, eachclass):
info = {}
info['title'] = re.search('title="(.*?)\"', eachclass, re.S).group(1)
print info['title']
info['content'] = re.search('<p style=\"height: 0px; opacity: 0; display: none;\">(.*?)</p>', eachclass,
re.S).group(1)
timeandlevel = re.findall('<em>(.*?)</em>', eachclass, re.S)
info['classtime'] = timeandlevel[0]
info['classlevel'] = timeandlevel[1]
info['learnnum'] = re.search('"learn-number">(.*?)</em>', eachclass, re.S).group(1)
return info
# saveinfo用来保存结果到info.txt文件中
def saveinfo(self, classinfo):
f = open('info.txt', 'a')
# for each in classinfo:
for i in range(len(classinfo)):
each = classinfo[i]
f.writelines("Lesson number: " + str(i) + '\n')
f.writelines('title:' + each['title'].encode("utf-8").replace("\t", "").replace("\n", "") + '\n')
f.writelines('content:' + each['content'].encode("utf-8").replace("\t", "").replace("\n", "") + '\n')
f.writelines('classtime:' + each['classtime'].encode("utf-8").replace("\t", "").replace("\n", "") + '\n')
f.writelines('classlevel:' + each['classlevel'].encode("utf-8").replace("\t", "").replace("\n", "") + '\n')
f.writelines('learnnum:' + each['learnnum'].encode("utf-8").replace("\t", "").replace("\n", "") + '\n\n\n')
f.close()
if __name__ == '__main__':
classinfo = []
url = 'http://www.jikexueyuan.com/course/?pageNum=1'
jikespider = spider()
all_links = jikespider.changepage(url, 10)
for link in all_links:
print u'正在处理页面:' + link
html = jikespider.getsource(link)
everyclass = jikespider.geteveryclass(html)
for each in everyclass:
info = jikespider.getinfo(each)
classinfo.append(info)
jikespider.saveinfo(classinfo)