还是那句话,文心4.0写文章不怎么样,写代码是一绝
不知道怎么优化的请加上处理JavaScript渲染的内容 (如果有的话),请加上设置爬取深度(可自己设置),同时加上一个优美的界面
要处理JavaScript渲染的内容,我们通常会使用如Selenium或Puppeteer这样的浏览器自动化工具,因为它们能够模拟一个真实的浏览器环境并执行JavaScript。然而,这将增加爬虫的复杂性并降低其效率。对于大多数现代网站,使用这些工具可能是必要的,因为很多内容是通过JavaScript动态加载的。
设置爬取深度可以通过在爬虫中添加一个深度计数器来实现。每当爬虫访问一个新页面时,深度计数器就会增加。当达到设定的最大深度时,爬虫将停止访问更多的页面。
至于优美的界面,我们可以使用Python的Tkinter库来创建一个简单的图形用户界面(GUI)。这个界面可以让用户输入起始URL、设置爬取深度等参数,并显示爬虫的运行状态。
以下是一个结合了上述功能的示例代码。请注意,这个示例使用了Selenium来处理JavaScript渲染的内容,并且使用了Tkinter来创建一个简单的GUI。
首先,确保你已经安装了必要的库:
bash复制代码pip install selenium beautifulsoup4 lxml tkinter
你还需要确保Selenium的WebDriver已经安装并配置在你的系统路径中。对于Chrome浏览器,你需要下载ChromeDriver。
然后,你可以使用以下代码:
import tkinter as tk
from tkinter import messagebox, simpledialog
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import threading
# 全局变量用于控制爬虫
stop_crawling = False
crawl_depth = 0
max_crawl_depth = 0
indexed_pages = []
visited_urls = set()
urls_to_visit = []
def crawl_website(start_url, depth):
global stop_crawling, crawl_depth, max_crawl_depth, indexed_pages, visited_urls, urls_to_visit
if depth > max_crawl_depth or stop_crawling:
return
options = Options()
options.headless = True # 在无头模式下运行,不显示浏览器窗口
driver = webdriver.Chrome(options=options) # 你可能需要指定chromedriver的路径,如:webdriver.Chrome('/path/to/chromedriver')
try:
driver.get(start_url)
soup = BeautifulSoup(driver.page_source, 'lxml')
# 提取页面标题和其他你需要的信息
title = soup.title.string if soup.title else 'No Title'
indexed_pages.append({'url': start_url, 'title': title})
print(f"Visited: {start_url}, Title: {title}")
# 提取页面上所有的链接并添加到待访问队列中(如果未达到最大深度)
for link in soup.find_all('a'):
href = link.get('href')
if href and not href.startswith('#'):
absolute_url = urljoin(start_url, href)
if absolute_url not in visited_urls:
visited_urls.add(absolute_url)
urls_to_visit.append((absolute_url, depth + 1))
# 递归地访问其他页面
while urls_to_visit and not stop_crawling:
next_url, next_depth = urls_to_visit.pop(0)
crawl_website(next_url, next_depth)
except Exception as e:
print(f"An error occurred: {e}")
finally:
driver.quit()
def start_crawling():
global stop_crawling, crawl_depth, max_crawl_depth, indexed_pages, visited_urls, urls_to_visit
start_url = entry_url.get()
max_crawl_depth = int(entry_depth.get())
if not start_url or max_crawl_depth < 1:
messagebox.showerror("Error", "Please enter a valid URL and a positive depth.")
return
stop_crawling = False
crawl_depth = 0
indexed_pages.clear()
visited_urls.clear()
urls_to_visit.clear()
urls_to_visit.append((start_url, 1))
# 使用线程来运行爬虫,以避免阻塞GUI
threading.Thread(target=crawl_website, args=(start_url, 1), daemon=True).start()
messagebox.showinfo("Crawling Started", "The crawler has started. Please wait until it finishes.")
def stop_crawling_wrapper():
global stop_crawling
stop_crawling = True
messagebox.showinfo("Crawling Stopped", "The crawler has been stopped.")
# 创建GUI界面
root = tk.Tk()
root.title("Web Crawler")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
label_url = tk.Label(frame, text="Start URL:")
label_url.grid(row=0, column=0, sticky="e")
entry_url = tk.Entry(frame, width=50)
entry_url.grid(row=0, column=1)
label_depth = tk.Label(frame, text="Max Depth:")
label_depth.grid(row=1, column=0, sticky="e")
entry_depth = tk.Entry(frame, width=10)
entry_depth.grid(row=1, column=1)
button_start = tk.Button(frame, text="Start Crawling", command=start_crawling)
button_start.grid(row=2, column=0, columnspan=2)
button_stop = tk.Button(frame, text="Stop Crawling", command=stop_crawling_wrapper)
button_stop.grid(row=3, column=0, columnspan=2)
root.mainloop()