import os
from lxml import etree
import requests,re
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
def all_text(url_name):
res=requests.get(url_name,headers=headers)
xiaoshuoming = re.findall('<meta property="og:novel:book_name" content="(.*?)"/>',res.text)[0]
if not os.path.exists(f'D:小说/{xiaoshuoming}'):
os.makedirs(f'D:小说/{xiaoshuoming}')
urls=re.findall('<dd><a href="(.*?)" >.*?</a></dd>',res.text)
names=re.findall('<dd><a href=".*?" >(.*?)</a></dd>',res.text)
return urls,names,xiaoshuoming
def download(url_name):
url='http://www.dvdspring.com'
urls,names,xiaoshuoming=all_text(url_name)
for i,j in zip(urls,names):
res_text=requests.get(url+i,headers=headers).text
zj_html=etree.HTML(res_text)
texts=zj_html.xpath('//*[@id="content"]/text()') #章节内容
texts2=zj_html.xpath('//*[@id="content"]/*/text()')
text='\n'.join(texts).strip()+'\n'.join(texts2).strip()
try:
with open(f'd:小说/{xiaoshuoming}/{j}.txt','a',encoding='utf8') as f:
f.write(text)
print(f'{xiaoshuoming}/{j}正在下载...')
except:
if j.startswith('?') or j.startswith('*') or '*' in j:
zhangjie=''.join(re.findall('\d',j))
with open(f'd:小说/{xiaoshuoming}/第{zhangjie}章.txt', 'a', encoding='utf8') as f:
f.write(text)
print(f'{xiaoshuoming}\t第{zhangjie}章 \t正在下载...')
if __name__ == '__main__':
url_name='http://www.dvdspring.com/html/7210/'#小说名地址
download(url_name)
#只需要更改代码末尾的url_name地址,在笔趣阁中任选一本小说,复制上面的网址 |