-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
54 lines (49 loc) · 1.37 KB
/
test.py
File metadata and controls
54 lines (49 loc) · 1.37 KB
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
#coding=utf-8
import re
import urllib2
from bs4 import BeautifulSoup as bs
#获取网页内容
def gethtml(url):
req = urllib2.Request(url)
response = urllib2.urlopen(req)
html = response.read()
return html
#获取文章主内容,使用BeautifulSoup
def getcontents(html):
soup = bs(html)
neirong = soup.find('div', attrs={'class': 'neirong'}).contents
return neirong
#获取每一章链接url
def gettitleurl(url):
html = gethtml(url)
reg = r'<li><a href=(.+?)title='
ulist = re.compile(reg)
urllist = ulist.findall(html)
return urllist
#获取每一章标题<h1>
def gettitle(url):
html = gethtml(url)
reg = r'<h1>(.+?)</h1>'
t = re.compile(reg)
title = t.findall(html)
return title
if __name__ == '__main__':
url = "https://www.51shucheng.com/kehuan/liucixinduanpian"
f = open('santi.txt', 'w+')
urllist = gettitleurl(url)
for i in urllist:
print i
url = i[1:len(i)-2]
print url
title = gettitle(url)
for j in title:
print 'downloading... '+str(j.decode('utf-8'))
f.write('\n\n'+j)
neirong = getcontents(gethtml(url))
for nr in neirong:
print type(nr)
print type(nr.string)
s = nr.string.encode('utf-8')
f.write(s)
print 'download success'
f.close()