整合如下两部分代码分别作为书源1和书源2在脚本内可供选择。给出完整代码import tkinter as tkfrom tkinter import ttkfrom tkinter import messageboxfrom seleniumwebdriverchromeoptions import Optionsfrom selenium import webdriverfrom seleniu
import tkinter as tk from tkinter import ttk from tkinter import messagebox from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.common.keys import Keys import time import os from lxml import etree import threading import requests from bs4 import BeautifulSoup
chapters = {} # 存储章节标题和内容的字典
def update_display(): listbox.delete(0, tk.END) for title in chapters: listbox.insert(tk.END, title)
def show_chapter_content(event): global chapter_text selected_title = listbox.get(listbox.curselection()) chapter_text.delete("1.0", tk.END) chapter_text.insert(tk.END, chapters[selected_title])
def crawl_novel1(keyword): # 创建浏览器Options对象,并设置无头模式 options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument("--window-size=1920,1080") options.add_argument("user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'")
# 创建浏览器对象
driver = webdriver.Chrome(options=options)
try:
# 打开笔趣阁小说网首页
driver.get("https://www.bige3.cc/")
# 输入搜索关键词并点击搜索按钮
search_input = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/input[1]")
search_input.send_keys(keyword)
# 按回车键进行搜索
search_input.send_keys(Keys.ENTER)
time.sleep(2)
# 依次点击小说标题打开小说
elements = driver.find_elements_by_xpath("/html/body/div[5]/div/div/div/div/div[2]/h4/a")
for i in range(len(elements)):
# 在这里执行你想要的操作,例如获取元素文本、点击元素等
elements = driver.find_elements_by_xpath("/html/body/div[5]/div/div/div/div/div[2]/h4/a")
element = elements[i]
element.click() # 点击元素
# 切换到新打开的窗口
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)
# 点击第一章标题
chapter = driver.find_elements_by_xpath("//*[@class='listmain']/dl/dd/a")[0]
chapter.click()
time.sleep(1)
while True:
# 获取章节标题
chapter_title = driver.find_element_by_xpath("//*[@id='read']/div[5]/div[3]/h1").text
# 获取章节内容
chapter_content = driver.find_element_by_xpath("//*[@id='chaptercontent']")
html = chapter_content.get_attribute("innerHTML")
# 使用lxml解析章节内容
tree = etree.HTML(html)
content = tree.xpath("string(.)") # 获取章节内容的文本形式
# 删除特定字段
chapter_title = chapter_title.replace("、","")
content = content.replace("无弹窗,更新快,免费阅读!", "")
content = content.replace("请收藏本站:https://www.bige3.cc。笔趣阁手机版:https://m.bige3.cc", "")
content = content.replace("『点此报错』『加入书签』", "")
if save_checkbox_var.get():
# 创建保存目录
book_dir = os.path.join(os.getcwd(), keyword)
if not os.path.exists(book_dir):
os.makedirs(book_dir)
# 保存章节内容到文件
chapter_path = os.path.join(book_dir, f"{chapter_title}.txt")
with open(chapter_path, "w", encoding="utf-8") as f:
f.write(content)
# 将章节标题和内容添加到字典中
chapters[chapter_title] = content
update_display() # 更新展示
try:
# 点击下一章按钮
next_button = driver.find_element_by_xpath('//div[@class="Readpage pagedown"]/a[@id="pb_next"]')
next_button.click()
time.sleep(1) # 等待页面加载
except:
break # 如果没有下一章按钮,则跳出循环
# 返回至小说列表页面
driver.back()
time.sleep(1)
except Exception as e:
messagebox.showinfo("完成", "运行完毕")
finally:
# 关闭浏览器
driver.quit()
if save_checkbox_var.get():
messagebox.showinfo("提示", "小说下载完成")
def crawl_novel2(novel_name): # Construct the search URL search_url = f"https://www.biquge66.net/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
first_result = search_soup.find("div", class_="image").find("a")
if first_result:
novel_href = first_result["href"]
# Construct the URL for the novel's main page
novel_url = f"https://www.biquge66.net{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
chapter_lists = soup.find_all("div", class_="flex flex-between book-info-main")
# Loop through each chapter link and extract chapter content
for chapter_list in chapter_lists:
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
# 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
chapter_content = chapter_soup.find("div", id="booktxt")
# Extract and print the first page of chapter content
if chapter_content:
chapter_text = chapter_content.text.replace("本站最新网址:www.biquge66.net", "") # Remove site info
# Check if there is a second page
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")
# If there is a second page, concatenate it with the first page
if chapter_content2:
chapter_text2 = chapter_content2.text.replace("本站最新网址:www.biquge66.net", "") # Remove site info
chapter_text += chapter_text2
# 将章节标题和内容添加到字典中
chapters[chapter_title] = chapter_text
update_display() # 更新展示
else:
print(f"Unable to retrieve chapter content: {chapter_title}\n")
else:
print(f"Failed to retriev
原文地址: https://www.cveoy.top/t/topic/iV68 著作权归作者所有。请勿转载和采集!