python下载斗罗大陆
时间: 2023-12-20 08:32:11 浏览: 31
以下是一个用Python下载斗罗大陆小说的示例代码:
```python
import requests
import os
def download_novel(url):
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
novel_title = html.split('<h1>')[1].split('</h1>')[0] # 获取小说标题
novel_dir = './novels/' + novel_title # 创建小说目录
if not os.path.exists(novel_dir):
os.makedirs(novel_dir)
chapter_list = html.split('<dd>') # 获取章节列表
for chapter in chapter_list[1:]:
chapter_title = chapter.split('<a ')[1].split('>')[1].split('<')[0] # 获取章节标题
chapter_url = chapter.split('<a ')[1].split('href="')[1].split('"')[0] # 获取章节链接
chapter_response = requests.get(url + chapter_url)
chapter_response.encoding = 'utf-8'
chapter_html = chapter_response.text
chapter_content = chapter_html.split('<div id="content">')[1].split('</div>')[0] # 获取章节内容
with open(novel_dir + '/' + chapter_title + '.txt', 'w', encoding='utf-8') as f:
f.write(chapter_content) # 将章节内容写入文件
# 调用函数下载斗罗大陆小说
url = 'http://www.biquku.la/0/421/'
download_novel(url)
```
请注意,这只是一个示例代码,具体的网页结构和解析方式可能会有所不同。你需要根据实际情况进行调整。