2025年4月21日 星期一 乙巳(蛇)年 正月廿二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

【干货】Python下载网络小说

时间:03-09来源:作者:点击数:41

【干货】Python下载网络小说

  • import requests
  • import time
  • from tqdm import tqdm
  • from bs4 import BeautifulSoup
  • def get_content(target):
  • req = requests.get(url = target)
  • req.encoding = 'utf-8'
  • html = req.text
  • bf = BeautifulSoup(html, 'lxml')
  • texts = bf.find('div', id='content')
  • content = texts.text.strip().split('\xa0'*4)
  • return content
  • if __name__ == '__main__':
  • server = 'https://www.xsbiquge.com'
  • book_name = '诡秘之主.txt'
  • target = 'https://www.xsbiquge.com/15_15338/'
  • req = requests.get(url = target)
  • req.encoding = 'utf-8'
  • html = req.text
  • chapter_bs = BeautifulSoup(html, 'lxml')
  • chapters = chapter_bs.find('div', id='list')
  • chapters = chapters.find_all('a')
  • for chapter in tqdm(chapters):
  • chapter_name = chapter.string
  • url = server + chapter.get('href')
  • content = get_content(url)
  • with open(book_name, 'a', encoding='utf-8') as f:
  • f.write(chapter_name)
  • f.write('\n')
  • f.write('\n'.join(content))
  • f.write('\n')
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门