f1 = open('test1', 'r+')
f2 = open('test2', 'a+')
for line in f1.readlines():
print(len(line))
if len(line) < 10:
line = '第{}个\n'.format(line.strip())
f2.writelines(line)
f1.close()
f2.close()
爬取网站http://www.cdzdgw.com/的小说,需要指定小说的主页地址:
例如:http://www.cdzdgw.com/28_28570/,低维游戏
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
import requests
response = requests.get("http://www.cdzdgw.com/28_28570/")
data = response.content.decode("gbk").split('\n')
chapter_list = []
for line in data:
line = line.strip('\r').strip('\t').strip(' ')
if line.endswith('</a></dd>'):
line = line.strip('<dd><a href=').strip('</a></dd>').replace('"', '').split(">")
line[0] = 'http://www.cdzdgw.com' + line[0].replace("'", '').strip(' ')
chapter_list.append(line)
f2 = open('test02.txt', 'a', encoding='gbk')
n = 1
for chapter in chapter_list[9:]:
chapter_url = chapter[0]
chapter_name = chapter[1]
response_chapter = requests.get(chapter_url)
chapter_data = response_chapter.content.decode("gbk").split('\n')
f2.write('\n' + chapter_name + '\n\n')
for chapter_line in chapter_data[58:]:
chapter_line = chapter_line.strip('\n').strip('\r').strip('\t').strip(' ')
if chapter_line.endswith('<br />') and len(chapter_line) > 10:
data = chapter_line.replace(' ', '').replace('<br />', '').replace('<div id="content">', '') + '\n'
f2.write(data)
n += 1
print(n)
f2.close()