Python下载小说huo

#import utilib
import requests, os
from bs4 import BeautifulSoup
url = 'https://www.biquge.com.cn/book/23341/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
booktitle = soup.find('h1').text
if not os.path.isdir(booktitle): # 判断当前文件夹下是否存在和小说名同名的文件夹
    os.makedirs(booktitle) # 若不存在,则创建小说同名文件夹
dd = soup.find_all('dd')
for i in range(len(dd)):
    if i < 30:
        title = dd[i].find('a').text
        chap_url = dd[i].find('a')['href']
        response1 = requests.get('https://www.biquge.com.cn' + chap_url)
        soup1 = BeautifulSoup(response1.content, 'lxml')
        text = soup1.find('div', id='content').text
        f = open(booktitle + '/' + title + '.txt', 'a+', encoding='utf-8')
        f.write(text)
        print("正在下载《 {} 》...... {} / {} ".format(title, i+1, len(dd)))
    else:
        break
print('本次共下载 {} 章, 保存地址:{}'.format(i, os.getcwd() + '\\' + booktitle + '\\'))

cnliutz

计算机基础教师

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注