倚天屠龙记小说网址:https://www.2biqukan.com/fiction/zsczu.html
实现代码:
from urllib import request
from bs4 import BeautifulSoup
import time
import socket
def download_specified_chapter(chapter_url, header, coding, chapter_name=None):
#先生成一个request对象,传入url和headers
download_req = request.Request(chapter_url,headers=header)
#通过指定urlopen打开request对象中的url网址,并获得对应内容
response = request.urlopen(download_req)
#获取页面的html
download_html = response.read().decode(coding, 'ignore')
#request.close()#记得要关闭
#获取html的bs
origin_soup = BeautifulSoup(download_html, 'lxml')
#获取小说正文部分
content=origin_soup.find(id='novel-content', class_='font-size-middle')
#整理小说格式,将\xa0替换成回车
# html中的 ,在转换成文档后,变成\xa0
txt=content.text.replace('\xa0'*8,'\n')
# 将获得的正文 写入txt
print("正在下载 {} 链接:{}".format(chapter_name,chapter_ur