小说下载器 - 获取热门小说章节内容
import requests from bs4 import BeautifulSoup import tkinter as tk from tkinter import ttk from tkinter import messagebox import threading
chapters = {}
def crawl_novel(source): # Input the novel name novel_name = entry.get()
# Construct the search URL
if source == 1:
search_url = f"https://www.biquge66.net/search/?searchkey={novel_name}"
else:
search_url = f"https://www.bige3.cc/search/?searchkey={novel_name}"
# Send an HTTP GET request to the search page
search_response = requests.get(search_url)
# Check if the request was successful
if search_response.status_code == 200:
# Parse the search page content
search_soup = BeautifulSoup(search_response.content, "html.parser")
# Find the first search result and extract its href
if source == 1:
first_result = search_soup.find('div', class_='image').find('a')
else:
first_result = search_soup.find('div', class_='book-result').find('li').find('a')
if first_result:
if source == 1:
novel_href = first_result['href']
# Construct the URL for the novel's main page
novel_url = f"https://www.biquge66.net{novel_href}"
else:
novel_href = first_result['href']
novel_url = f"https://www.bige3.cc{novel_href}"
# Send an HTTP GET request to the novel's main page
response = requests.get(novel_url)
# Check if the request was successful
if response.status_code == 200:
# Parse the novel's main page content
soup = BeautifulSoup(response.content, "html.parser")
# Find all the chapter links
if source == 1:
chapter_lists = soup.find_all('div', class_='flex flex-between book-info-main')
else:
chapter_lists = soup.find_all('dl')
# Loop through each chapter link and extract chapter content
for chapter_list in chapter_lists:
if source == 1:
for chapter in chapter_list.find_all('a', rel='chapter'):
chapter_title = chapter.text # Get chapter title
chapter_url = "https://www.biquge66.net" + chapter['href'] # Build complete chapter URL
else:
for chapter in chapter_list.find_all('a'):
chapter_title = chapter['title'] # Get chapter title
chapter_url = "https://www.bige3.cc" + chapter['href'] # Build complete chapter URL
# Send an HTTP GET request to get the first page of the chapter content
chapter_response = requests.get(chapter_url)
chapter_soup = BeautifulSoup(chapter_response.content, "html.parser")
# Find the first page of chapter content
if source == 1:
chapter_content = chapter_soup.find('div', id='booktxt')
else:
chapter_content = chapter_soup.find('div', id='chaptercontent')
# Extract and print the first page of chapter content
if chapter_content:
if source == 1:
chapter_text = chapter_content.text.replace("本站最新网址:www.biquge66.net", "") # Remove site info
else:
chapter_text = chapter_content.text.replace("无弹窗,更新快,免费阅读!", "") # Remove site info
# Check if there is a second page
if source == 1:
chapter_url2 = chapter_url.replace(".html", "_2.html")
chapter_response2 = requests.get(chapter_url2)
chapter_soup2 = BeautifulSoup(chapter_response2.content, "html.parser")
chapter_content2 = chapter_soup2.find('div', id='booktxt')
else:
chapter_url2 = chapter_url.replace(".html", "_2.html")
chapter_response2 = requests.get(chapter_url2)
chapter_soup2 = BeautifulSoup(chapter_response2.content, "html.parser")
chapter_content2 = chapter_soup2.find('div', id='chaptercontent')
# If there is a second page, concatenate it with the first page
if chapter_content2:
if source == 1:
chapter_text2 = chapter_content2.text.replace("本站最新网址:www.biquge66.net", "") # Remove site info
else:
chapter_text2 = chapter_content2.text.replace("无弹窗,更新快,免费阅读!", "") # Remove site info
chapter_text += chapter_text2
chapters[chapter_title] = chapter_text
update_display()
else:
messagebox.showinfo("提示", f"无法获取章节内容: {chapter_title}")
else:
messagebox.showinfo("提示", f"无法获取小说页面: {novel_url}")
else:
messagebox.showinfo("提示", "未找到相关搜索结果")
else:
messagebox.showinfo("提示", f"无法获取搜索结果页面: {search_url}")
def update_display(): listbox.delete(0, tk.END) for title in chapters: listbox.insert(tk.END, title)
def show_chapter_content(event): selected_title = listbox.get(listbox.curselection()) chapter_text.delete("1.0", tk.END) chapter_text.insert(tk.END, chapters[selected_title])
def search_novel(): threading.Thread(target=crawl_novel, args=(source_combobox.current(),)).start()
root = tk.Tk() root.title("小说下载器") root.geometry("600x400")
Using ttk style to set widget appearance
style = ttk.Style()
Set window appearance
root.configure(bg='white') style.configure('TFrame', background='white')
Set label appearance
style.configure('TLabel', background='white', font=('Arial', 13))
Set button appearance
style.configure('TButton', background='lightblue', font=('Arial', 13), foreground='green')
Set combobox appearance
style.configure('TCombobox', background='white', font=('Arial', 13))
Set listbox appearance
style.configure('TListbox', background='white', font=('Arial', 12))
Set text appearance
style.configure('TText', background='white', font=('Arial', 13))
label = ttk.Label(root, text="请输入小说名称:") label.pack()
entry = ttk.Entry(root) entry.config(font=13) entry.pack()
source_label = ttk.Label(root, text="请选择书源:") source_label.pack()
source_combobox = ttk.Combobox(root, values=["笔趣阁", "大笔趣阁"]) source_combobox.current(0) source_combobox.pack()
button = ttk.Button(root, text="获取", command=search_novel) button.pack()
listbox_frame = ttk.Frame(root) listbox_frame.pack(side=tk.LEFT, fill=tk.Y)
scrollbar = ttk.Scrollbar(listbox_frame) scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox = tk.Listbox(listbox_frame, selectmode=tk.SINGLE, yscrollcommand=scrollbar.set) listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=listbox.yview)
listbox.bind("<
chapter_text_frame = ttk.Frame(root) chapter_text_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
chapter_text_scrollbar = ttk.Scrollbar(chapter_text_frame) chapter_text_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
chapter_text = tk.Text(chapter_text_frame, yscrollcommand=chapter_text_scrollbar.set) chapter_text.config(spacing1=10, spacing2=20) chapter_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
chapter_text_scrollbar.config(command=chapter_text.yview)
root.mainloop()
原文地址: https://www.cveoy.top/t/topic/qB8N 著作权归作者所有。请勿转载和采集!