import requests
import time
from bs4 import BeautifulSoup
from tqdm import tqdm
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.39'
}
def crawl_chapter(book_url, file, titles):
response = requests.get(book_url, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'lxml')
content = soup.find(attrs={'id': 'chaptercontent'})
if not content:
return
content = content.get_text()
title = soup.h1.string
if title in titles:
return
titles.add(title)
file.write(f'{title}\n\n{content}\n\n')
time.sleep(3)
if __name__ == '__main__':
with open('大秦:秦始皇能听到我的心声最新章节列表.txt', 'a', encoding='utf-8') as f:
titles = set()
for i in tqdm(range(1, 472)):
# 网站链接
base_url = 'https://www.qimao5.com'
# 书籍链接
book_url = f'https://www.qimao5.com/book/144498/{i}.html'
# 匹配章节链接的正则表达式
crawl_chapter(book_url, f, titles)