#我个人比较喜欢看网文小说,也十分感谢笔**阁提供的平台,但是如果能以txt格式下载到本地,听书肯定对看了一天电脑的眼睛比较好,所以就研究了一下,接下来,开薅!
一、章节目录信息
找到一本我们爱看的书籍的具体页面,打开开发者工具,刷新页面,抓包。
然后我们不难发现在第一条中,用get请求得到了章节目录信息
而且是没有负载的,那么也就是说所有的信息就在url里,再试试其他书籍我们不难发现每本书的https://www.bg70.cc/book/2749/book后的数字是不一样的,所以这就是每本书的独立标识。通过这个数字我们就能找到这本书咯。
二、每一个章节的文本信息
点开一章,抓包!
同样的没有负载,对于网址https://www.bg70.cc/book/2749/2.html其实就是在书籍详情页后加上一个章节的索引。
到此分析结束!接下来就是如和把我们需要的文本信息如和完整准确的弄下来了!
三、所需文本提取工具
通过python的requests的get方法得到会是整个HTML页面,这肯定不是我们想要的。这时候我们就需要借助一个python用于处理这种页面的库——bs4的 BeautifulSoup库。这样我们就可以通过特定对象的一些属性来得到它,起到过滤的作用。
四、项目部署与实现
(一)得到章节列表等信息
在数据详情页,可以得到书籍的章节列表、每一章的部分网址以及书名
章节是在<div class="listmain">容器下的一些dd里,拿到这个dd的href属性就可以了,将其存储在一个列表中返回,标题同理。
那么我们就拿整个部分网址以及书名作为后续操作的数据。
(二)章节文本信息提取
在详情页得到网址后可以加以拼接,得到每一页的网址,拿去请求就得到每一页的HTML文本。
而我们需要的文本都是在 <div id="chaptercontent" class="Readarea ReadAjax_content">这个容器里的,拿到这个容器的text文本就结束了。
(三)上具体代码
我提供一个简单的示例,能跑,但还不是多线程,后续还会更新多线程版本!
import json
import requests
import time
from bs4 import BeautifulSoup
import re
def get_manu(num):
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9",
"referer": "https://www.bg70.cc/book/{}/".format(num),
"^sec-ch-ua": "^\\^Google",
"sec-ch-ua-mobile": "?0",
"^sec-ch-ua-platform": "^\\^Windows^^^",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
}
times = int(time.time())
cookies = {
"Hm_lvt_2254e433738fcc1aed329cee161b448a": str(times),
"Hm_lpvt_2254e433738fcc1aed329cee161b448a": str(times + 15)
}
url = "https://www.bg70.cc/book/{}/".format(num)
response = requests.get(url, headers=headers, cookies=cookies)
soup = BeautifulSoup(response.text, 'lxml')
title = soup.find("span", class_="title").text
print(title)
tags = soup.find('div', class_='listmain').find("dl").findAll('dd')
tags_url = []
for tag in tags:
# 从<dd>标签中提取<a>标签
a_tag = tag.find('a').get('href')
tags_url.append(a_tag)
# 获取<a>标签的href属性值
tag = tags[-1]
numbers = re.findall(r'\d+', str(tag.text))
return tags_url, numbers[-1], title
def save(urls: list, num, titles):
for url in urls:
try:
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9",
"referer": "https://www.bg70.cc/book/{}/".format(num),
"sec-ch-ua": "^\\^Google",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\\^Windows^^^",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
}
times = int(time.time())
cookies = {
"Hm_lvt_2254e433738fcc1aed329cee161b448a": str(times),
"Hm_lpvt_2254e433738fcc1aed329cee161b448a": str(times + 16)
}
url = "https://www.bg70.cc{}".format(url)
response = requests.get(url, headers=headers, cookies=cookies)
soup = BeautifulSoup(response.text, 'lxml')
title = soup.find('h1', class_='wap_none').text
print(title)
text = soup.find("div", class_="Readarea ReadAjax_content").text.replace(
"请收藏本站:https://www.bg70.cc。笔趣阁手机版:https://m.bg70.cc", "").replace("『点此报错』『加入书签』",
"").replace(" ", "\n")
print(text)
with open(f"{titles}.txt", "a", encoding="utf-8") as novel:
novel.write(title + '\n')
novel.write(text)
time.sleep(1)
except:
pass
if __name__ == "__main__":
book_number = str(input("输入书号!\n"))
urls_list, count, title = get_manu(book_number)
print("本次爬取的目标书目为:{},共有{}章".format(title, str(count)))
save(urls_list, book_number, title)
使用时只需要把浏览到书籍网址时,book后就是该书籍的具体书号,输入该书号就可以得到txt文本了!