#-*-coding:utf-8-*-
# coding:utf-8
#导入模块
import os
import requests
from pyquery import PyQuery as body
#抓取地址
url = "https://www.xbiquge.la"
# 抓取小说 [笔趣阁] 函数
def xiaosuo():
#判断目录xiaosuo是否存在
if not os.path.isdir(os.getcwd() + '/xiaosuo'):
#不存在则创建
os.mkdir(os.getcwd()+'/xiaosuo')
#获取源码并转成jquery语法
jquery = body(requests.get(url + '/xiaoshuodaquan/').content, parser='html')
#获取目录所有(小说书名) 地址
for a in jquery('.novellist').find('ul').find('li').items():
#判断目录(小说书名) 是否存在
if not os.path.isdir(os.getcwd() + '/xiaosuo/' + a.find('a').text()):
#不存在则创建
os.mkdir(os.getcwd() + '/xiaosuo/' + a.find('a').text())
#抓取每本小说
jquery = body(requests.get(a.find('a').attr('href')).content, parser='html')
#获取每本小说
for b in jquery('#list dd').items():
#获取源码并转成jquery语法格式
jquery = body(requests.get(url + b.find('a').attr('href')).content, parser='html')
#写入文本
open(os.getcwd() + '/xiaosuo/' + a.find('a').text() + '/' + b.find('a').text() + '.txt', 'w').write(jquery('#content').remove('p').text())
#调用函数
xiaosuo()
05-19
443
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-17
411
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)