编程语言
首页 > 编程语言> > 笔趣阁学习源码

笔趣阁学习源码

作者:互联网

# -*- coding:utf-8 -*-
"""
源码仅供学习交流使用,适用于PYTHON3.10版
"""
import requests
import pprint
import parsel
from lxml import etree
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
url = 'https://www.bbiquge.net/book/133312/'
response = requests.get(url=url)
# pprint.pprint(response.text)
page_text = etree.HTML(response.text)
xiaoshuo_name = page_text.xpath('//*[@id="info"]/h1/text()')[0] # 提取小说名
href = page_text.xpath('/html/body/div[4]/dl//a/@href') #小说章节href连接
# print(href)
for link in href:
link_url = 'https://www.bbiquge.net/book/133312/' + link
page_text1 = requests.get(url=link_url)
# url = 'https://www.bbiquge.net/book/133312/56524592.html'
page_text2 = etree.HTML(page_text1.text)
章节名 = page_text2.xpath('//*[@id="main"]/h1/text()')[0] # 提取章节名
    章节内容列表 = page_text2.xpath('//*[@id="content"]//text()') [1:] # 提取章节内容
    章节内容 = '\n'.join(章节内容列表)
# print(章节名+'\n',章节内容)
with open(xiaoshuo_name+'.txt','a',encoding='utf-8') as fp:
fp.write(章节名)
fp.write('\n')
fp.write(章节内容)
fp.write('\n')
print(章节名)
#////*[@id="content"]

标签:章节,fp,url,text,page,学习,源码,href,笔趣
来源: https://www.cnblogs.com/king888/p/16503462.html