-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.py
More file actions
1402 lines (1228 loc) · 51.7 KB
/
Copy pathgui.py
File metadata and controls
1402 lines (1228 loc) · 51.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import sys
from pathlib import Path
from typing import Optional
from datetime import datetime
import markdown
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QTextEdit,
QGroupBox,
QCheckBox,
QDoubleSpinBox,
QTabWidget,
QFileDialog,
QMessageBox,
QProgressBar,
QSpinBox,
QComboBox,
QScrollArea,
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from PyQt6.QtGui import QFont
from PyQt6.QtWebEngineWidgets import QWebEngineView
from crawl4ai import (
AsyncWebCrawler,
BrowserConfig,
CrawlerRunConfig,
UndetectedAdapter,
)
from crawl4ai.async_crawler_strategy import AsyncPlaywrightCrawlerStrategy
# 深度爬取相关导入
try:
from crawl4ai.deep_crawling import (
BFSDeepCrawlStrategy,
DFSDeepCrawlStrategy,
BestFirstCrawlingStrategy,
)
from crawl4ai.deep_crawling.filters import (
FilterChain,
URLPatternFilter,
DomainFilter,
ContentTypeFilter,
)
from crawl4ai.deep_crawling.scorers import KeywordRelevanceScorer
DEEP_CRAWL_AVAILABLE = True
except ImportError:
DEEP_CRAWL_AVAILABLE = False
class CrawlerWorker(QThread):
"""爬虫工作线程"""
finished = pyqtSignal(object)
error = pyqtSignal(str)
status_update = pyqtSignal(str)
def __init__(self, url: str, browser_config: BrowserConfig, crawler_config: CrawlerRunConfig):
super().__init__()
self.url = url
self.browser_config = browser_config
self.crawler_config = crawler_config
def run(self):
"""执行爬虫任务"""
try:
self.status_update.emit("正在初始化爬虫...")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async def crawl():
strategy = AsyncPlaywrightCrawlerStrategy(
browser_config=self.browser_config,
browser_adapter=UndetectedAdapter()
)
async with AsyncWebCrawler(
crawler_strategy=strategy,
config=self.browser_config
) as crawler:
self.status_update.emit(f"正在访问 {self.url}...")
# 检查是否是深度爬取
if self.crawler_config.deep_crawl_strategy:
# 深度爬取模式
if self.crawler_config.stream:
# 流式模式
results = []
async for result in await crawler.arun(self.url, config=self.crawler_config):
results.append(result)
self.status_update.emit(f"已爬取 {len(results)} 个页面...")
return results
else:
# 非流式模式
results = await crawler.arun(self.url, config=self.crawler_config)
self.status_update.emit(f"爬取完成,共 {len(results)} 个页面")
return results
else:
# 单页爬取模式
result = await crawler.arun(self.url, config=self.crawler_config)
return result
result = loop.run_until_complete(crawl())
loop.close()
self.finished.emit(result)
except Exception as e:
self.error.emit(str(e))
class WebCrawlerGUI(QMainWindow):
"""网络爬虫GUI主窗口"""
def __init__(self):
super().__init__()
self.worker: Optional[CrawlerWorker] = None
self.current_result = None
self.is_dark_mode = False # 默认亮色模式
self.init_ui()
def init_ui(self):
"""初始化用户界面"""
self.setWindowTitle("网络爬虫工具 - Web Crawler")
self.setGeometry(100, 100, 1200, 800)
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
# URL输入区域
url_group = QGroupBox("目标URL")
url_layout = QHBoxLayout()
url_layout.addWidget(QLabel("网址:"))
self.url_input = QLineEdit()
self.url_input.setPlaceholderText("https://www.example.com/")
self.url_input.setText("https://www.osredm.com/")
url_layout.addWidget(self.url_input)
main_layout.addWidget(url_group)
url_group.setLayout(url_layout)
# 配置选项区域
config_group = QGroupBox("爬虫配置")
config_layout = QVBoxLayout()
# 浏览器配置
browser_layout = QHBoxLayout()
self.headless_checkbox = QCheckBox("无头模式 (Headless)")
self.headless_checkbox.setChecked(False)
browser_layout.addWidget(self.headless_checkbox)
self.verbose_checkbox = QCheckBox("详细输出 (Verbose)")
self.verbose_checkbox.setChecked(True)
browser_layout.addWidget(self.verbose_checkbox)
browser_layout.addStretch()
config_layout.addLayout(browser_layout)
# 爬虫运行配置
crawler_layout = QHBoxLayout()
crawler_layout.addWidget(QLabel("延迟时间 (秒):"))
self.delay_spinbox = QDoubleSpinBox()
self.delay_spinbox.setRange(0.0, 60.0)
self.delay_spinbox.setValue(5.0)
self.delay_spinbox.setSingleStep(0.5)
crawler_layout.addWidget(self.delay_spinbox)
self.simulate_user_checkbox = QCheckBox("模拟用户行为")
self.simulate_user_checkbox.setChecked(True)
crawler_layout.addWidget(self.simulate_user_checkbox)
self.magic_checkbox = QCheckBox("魔法模式")
self.magic_checkbox.setChecked(True)
crawler_layout.addWidget(self.magic_checkbox)
self.wait_images_checkbox = QCheckBox("等待图片加载")
self.wait_images_checkbox.setChecked(True)
crawler_layout.addWidget(self.wait_images_checkbox)
crawler_layout.addStretch()
config_layout.addLayout(crawler_layout)
# 深度爬取配置
deep_crawl_layout = QVBoxLayout()
self.enable_deep_crawl_checkbox = QCheckBox("启用深度爬取")
self.enable_deep_crawl_checkbox.setChecked(False)
self.enable_deep_crawl_checkbox.toggled.connect(self._on_deep_crawl_toggled)
deep_crawl_layout.addWidget(self.enable_deep_crawl_checkbox)
# 深度爬取选项容器(默认隐藏)
self.deep_crawl_options = QWidget()
deep_crawl_options_layout = QVBoxLayout(self.deep_crawl_options)
deep_crawl_options_layout.setContentsMargins(20, 10, 10, 10)
# 策略选择
strategy_layout = QHBoxLayout()
strategy_layout.addWidget(QLabel("爬取策略:"))
self.strategy_combo = QComboBox()
self.strategy_combo.addItems(["BFS (广度优先)", "DFS (深度优先)", "BestFirst (最佳优先)"])
strategy_layout.addWidget(self.strategy_combo)
strategy_layout.addStretch()
deep_crawl_options_layout.addLayout(strategy_layout)
# 深度和页面限制
limits_layout = QHBoxLayout()
limits_layout.addWidget(QLabel("最大深度:"))
self.max_depth_spinbox = QSpinBox()
self.max_depth_spinbox.setRange(1, 10)
self.max_depth_spinbox.setValue(2)
limits_layout.addWidget(self.max_depth_spinbox)
limits_layout.addWidget(QLabel("最大页面数:"))
self.max_pages_spinbox = QSpinBox()
self.max_pages_spinbox.setRange(1, 1000)
self.max_pages_spinbox.setValue(50)
self.max_pages_spinbox.setSpecialValueText("无限制")
limits_layout.addWidget(self.max_pages_spinbox)
limits_layout.addStretch()
deep_crawl_options_layout.addLayout(limits_layout)
# 其他选项
options_layout = QHBoxLayout()
self.include_external_checkbox = QCheckBox("包含外部链接")
self.include_external_checkbox.setChecked(False)
options_layout.addWidget(self.include_external_checkbox)
self.stream_results_checkbox = QCheckBox("流式输出")
self.stream_results_checkbox.setChecked(True)
self.stream_results_checkbox.setToolTip("实时显示爬取结果,而不是等待所有页面完成")
options_layout.addWidget(self.stream_results_checkbox)
options_layout.addStretch()
deep_crawl_options_layout.addLayout(options_layout)
# URL过滤器
filter_group = QGroupBox("URL过滤器")
filter_layout = QVBoxLayout()
# URL模式
url_pattern_layout = QHBoxLayout()
url_pattern_layout.addWidget(QLabel("URL模式 (用逗号分隔):"))
self.url_pattern_input = QLineEdit()
self.url_pattern_input.setPlaceholderText("例如: *blog*, *docs*, *guide*")
url_pattern_layout.addWidget(self.url_pattern_input)
filter_layout.addLayout(url_pattern_layout)
# 允许的域名
allowed_domain_layout = QHBoxLayout()
allowed_domain_layout.addWidget(QLabel("允许的域名 (用逗号分隔):"))
self.allowed_domains_input = QLineEdit()
self.allowed_domains_input.setPlaceholderText("例如: example.com, docs.example.com")
allowed_domain_layout.addWidget(self.allowed_domains_input)
filter_layout.addLayout(allowed_domain_layout)
# 阻止的域名
blocked_domain_layout = QHBoxLayout()
blocked_domain_layout.addWidget(QLabel("阻止的域名 (用逗号分隔):"))
self.blocked_domains_input = QLineEdit()
self.blocked_domains_input.setPlaceholderText("例如: old.example.com")
blocked_domain_layout.addWidget(self.blocked_domains_input)
filter_layout.addLayout(blocked_domain_layout)
filter_group.setLayout(filter_layout)
deep_crawl_options_layout.addWidget(filter_group)
# 关键词评分器(用于BestFirst策略)
scorer_group = QGroupBox("关键词评分器 (BestFirst策略)")
scorer_layout = QVBoxLayout()
keyword_layout = QHBoxLayout()
keyword_layout.addWidget(QLabel("关键词 (用逗号分隔):"))
self.keywords_input = QLineEdit()
self.keywords_input.setPlaceholderText("例如: crawl, example, async, configuration")
keyword_layout.addWidget(self.keywords_input)
scorer_layout.addLayout(keyword_layout)
weight_layout = QHBoxLayout()
weight_layout.addWidget(QLabel("权重:"))
self.scorer_weight_spinbox = QDoubleSpinBox()
self.scorer_weight_spinbox.setRange(0.0, 1.0)
self.scorer_weight_spinbox.setValue(0.7)
self.scorer_weight_spinbox.setSingleStep(0.1)
weight_layout.addWidget(self.scorer_weight_spinbox)
weight_layout.addStretch()
scorer_layout.addLayout(weight_layout)
scorer_group.setLayout(scorer_layout)
deep_crawl_options_layout.addWidget(scorer_group)
# 评分阈值(用于BFS/DFS策略)
threshold_layout = QHBoxLayout()
threshold_layout.addWidget(QLabel("评分阈值 (BFS/DFS策略):"))
self.score_threshold_spinbox = QDoubleSpinBox()
self.score_threshold_spinbox.setRange(-1.0, 1.0)
self.score_threshold_spinbox.setValue(0.0)
self.score_threshold_spinbox.setSingleStep(0.1)
self.score_threshold_spinbox.setSpecialValueText("无限制")
threshold_layout.addWidget(self.score_threshold_spinbox)
threshold_layout.addStretch()
deep_crawl_options_layout.addLayout(threshold_layout)
deep_crawl_layout.addWidget(self.deep_crawl_options)
self.deep_crawl_options.setVisible(False)
config_layout.addLayout(deep_crawl_layout)
config_group.setLayout(config_layout)
main_layout.addWidget(config_group)
# 控制按钮
button_layout = QHBoxLayout()
self.start_button = QPushButton("开始爬取")
self.start_button.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
font-weight: bold;
padding: 8px 16px;
border-radius: 4px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:disabled {
background-color: #cccccc;
}
""")
self.start_button.clicked.connect(self.start_crawling)
button_layout.addWidget(self.start_button)
self.stop_button = QPushButton("停止")
self.stop_button.setEnabled(False)
self.stop_button.setStyleSheet("""
QPushButton {
background-color: #f44336;
color: white;
font-weight: bold;
padding: 8px 16px;
border-radius: 4px;
}
QPushButton:hover {
background-color: #da190b;
}
QPushButton:disabled {
background-color: #cccccc;
}
""")
self.stop_button.clicked.connect(self.stop_crawling)
button_layout.addWidget(self.stop_button)
self.save_button = QPushButton("保存结果")
self.save_button.setEnabled(False)
self.save_button.setStyleSheet("""
QPushButton {
background-color: #2196F3;
color: white;
font-weight: bold;
padding: 8px 16px;
border-radius: 4px;
}
QPushButton:hover {
background-color: #1976D2;
}
QPushButton:disabled {
background-color: #cccccc;
color: #888;
}
""")
self.save_button.clicked.connect(self.save_results)
button_layout.addWidget(self.save_button)
# 主题切换按钮
self.theme_button = QPushButton("🌙 暗色模式")
self.theme_button.setToolTip("切换亮色/暗色主题")
self.theme_button.setStyleSheet("""
QPushButton {
background-color: #607D8B;
color: white;
font-weight: bold;
padding: 8px 16px;
border-radius: 4px;
}
QPushButton:hover {
background-color: #546e7a;
}
""")
self.theme_button.clicked.connect(self.toggle_theme)
button_layout.addWidget(self.theme_button)
button_layout.addStretch()
main_layout.addLayout(button_layout)
# 进度条
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 0) # 不确定进度
self.progress_bar.setVisible(False)
main_layout.addWidget(self.progress_bar)
# 状态标签
self.status_label = QLabel("就绪")
main_layout.addWidget(self.status_label)
# 结果区域 - 使用标签页
results_group = QGroupBox("爬取结果")
results_layout = QVBoxLayout()
# 结果摘要
summary_layout = QHBoxLayout()
summary_layout.addWidget(QLabel("状态码:"))
self.status_code_label = QLabel("-")
summary_layout.addWidget(self.status_code_label)
summary_layout.addWidget(QLabel("成功:"))
self.success_label = QLabel("-")
summary_layout.addWidget(self.success_label)
summary_layout.addWidget(QLabel("控制台消息:"))
self.console_count_label = QLabel("-")
summary_layout.addWidget(self.console_count_label)
summary_layout.addStretch()
results_layout.addLayout(summary_layout)
# 标签页显示不同内容
self.tab_widget = QTabWidget()
# Markdown渲染标签页(第一个,作为默认显示)
self.markdown_preview = QWebEngineView()
self.markdown_preview.setHtml(self._get_empty_html())
self.tab_widget.addTab(self.markdown_preview, "预览")
# Markdown源码标签页
self.markdown_text = QTextEdit()
self.markdown_text.setReadOnly(True)
self.markdown_text.setFont(QFont("Consolas", 10))
self.tab_widget.addTab(self.markdown_text, "Markdown源码")
# HTML标签页
self.html_text = QTextEdit()
self.html_text.setReadOnly(True)
self.html_text.setFont(QFont("Consolas", 10))
self.tab_widget.addTab(self.html_text, "HTML")
# 控制台消息标签页
self.console_text = QTextEdit()
self.console_text.setReadOnly(True)
self.console_text.setFont(QFont("Consolas", 10))
self.tab_widget.addTab(self.console_text, "控制台消息")
results_layout.addWidget(self.tab_widget)
results_group.setLayout(results_layout)
main_layout.addWidget(results_group)
# 应用初始主题
self.apply_theme()
def apply_theme(self):
"""应用当前主题样式"""
if self.is_dark_mode:
self._apply_dark_theme()
else:
self._apply_light_theme()
def _apply_light_theme(self):
"""应用亮色主题"""
self.setStyleSheet("""
QMainWindow {
background-color: #ffffff;
}
QGroupBox {
font-weight: bold;
border: 2px solid #e0e0e0;
border-radius: 8px;
margin-top: 12px;
padding-top: 12px;
background-color: #fafafa;
color: #333;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 12px;
padding: 0 8px;
color: #555;
}
QLineEdit {
padding: 8px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 13px;
background-color: #ffffff;
color: #333;
}
QLineEdit:focus {
border-color: #4CAF50;
}
QLabel {
color: #333;
}
QCheckBox {
spacing: 6px;
color: #555;
}
QCheckBox::indicator {
width: 18px;
height: 18px;
border: 2px solid #ddd;
border-radius: 3px;
background-color: #ffffff;
}
QCheckBox::indicator:checked {
background-color: #4CAF50;
border-color: #4CAF50;
}
QDoubleSpinBox {
padding: 6px;
border: 2px solid #ddd;
border-radius: 4px;
min-width: 80px;
background-color: #ffffff;
color: #333;
}
QDoubleSpinBox:focus {
border-color: #4CAF50;
}
QTabWidget::pane {
border: 1px solid #ddd;
border-radius: 4px;
background-color: white;
}
QTabBar::tab {
background-color: #f5f5f5;
color: #666;
padding: 8px 20px;
margin-right: 2px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
QTabBar::tab:selected {
background-color: white;
color: #4CAF50;
font-weight: bold;
}
QTabBar::tab:hover {
background-color: #e8f5e9;
}
QTextEdit {
border: 1px solid #ddd;
border-radius: 4px;
background-color: #fafafa;
color: #333;
}
QProgressBar {
border: 2px solid #ddd;
border-radius: 4px;
text-align: center;
height: 24px;
background-color: #f5f5f5;
}
QProgressBar::chunk {
background-color: #4CAF50;
border-radius: 2px;
}
""")
# 更新主题按钮文本
self.theme_button.setText("🌙 暗色模式")
# 更新状态标签颜色
if hasattr(self, 'status_label'):
current_text = self.status_label.text()
if "错误" in current_text or "✗" in current_text:
self.status_label.setStyleSheet("color: #f44336; padding: 4px;")
elif "完成" in current_text or "✓" in current_text:
self.status_label.setStyleSheet("color: #4CAF50; padding: 4px;")
elif "正在" in current_text or "⏳" in current_text:
self.status_label.setStyleSheet("color: #2196F3; padding: 4px;")
else:
self.status_label.setStyleSheet("color: #666; padding: 4px;")
def _apply_dark_theme(self):
"""应用暗色主题"""
self.setStyleSheet("""
QMainWindow {
background-color: #1e1e1e;
}
QGroupBox {
font-weight: bold;
border: 2px solid #404040;
border-radius: 8px;
margin-top: 12px;
padding-top: 12px;
background-color: #2d2d2d;
color: #e0e0e0;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 12px;
padding: 0 8px;
color: #b0b0b0;
}
QLineEdit {
padding: 8px;
border: 2px solid #404040;
border-radius: 4px;
font-size: 13px;
background-color: #2d2d2d;
color: #e0e0e0;
}
QLineEdit:focus {
border-color: #66bb6a;
}
QLabel {
color: #e0e0e0;
}
QCheckBox {
spacing: 6px;
color: #b0b0b0;
}
QCheckBox::indicator {
width: 18px;
height: 18px;
border: 2px solid #404040;
border-radius: 3px;
background-color: #2d2d2d;
}
QCheckBox::indicator:checked {
background-color: #66bb6a;
border-color: #66bb6a;
}
QDoubleSpinBox {
padding: 6px;
border: 2px solid #404040;
border-radius: 4px;
min-width: 80px;
background-color: #2d2d2d;
color: #e0e0e0;
}
QDoubleSpinBox:focus {
border-color: #66bb6a;
}
QTabWidget::pane {
border: 1px solid #404040;
border-radius: 4px;
background-color: #1e1e1e;
}
QTabBar::tab {
background-color: #2d2d2d;
color: #b0b0b0;
padding: 8px 20px;
margin-right: 2px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
QTabBar::tab:selected {
background-color: #1e1e1e;
color: #66bb6a;
font-weight: bold;
}
QTabBar::tab:hover {
background-color: #3d3d3d;
}
QTextEdit {
border: 1px solid #404040;
border-radius: 4px;
background-color: #1e1e1e;
color: #e0e0e0;
}
QProgressBar {
border: 2px solid #404040;
border-radius: 4px;
text-align: center;
height: 24px;
background-color: #2d2d2d;
color: #e0e0e0;
}
QProgressBar::chunk {
background-color: #66bb6a;
border-radius: 2px;
}
""")
# 更新主题按钮文本
self.theme_button.setText("☀️ 亮色模式")
# 更新状态标签颜色
if hasattr(self, 'status_label'):
current_text = self.status_label.text()
if "错误" in current_text or "✗" in current_text:
self.status_label.setStyleSheet("color: #ef5350; padding: 4px;")
elif "完成" in current_text or "✓" in current_text:
self.status_label.setStyleSheet("color: #66bb6a; padding: 4px;")
elif "正在" in current_text or "⏳" in current_text:
self.status_label.setStyleSheet("color: #42a5f5; padding: 4px;")
else:
self.status_label.setStyleSheet("color: #b0b0b0; padding: 4px;")
def toggle_theme(self):
"""切换主题"""
self.is_dark_mode = not self.is_dark_mode
self.apply_theme()
# 更新Markdown预览以应用新主题
if self.current_result and self.current_result.markdown and self.current_result.markdown.raw_markdown:
# 如果有当前结果,重新渲染Markdown
try:
html_content = self._render_markdown(self.current_result.markdown.raw_markdown)
self.markdown_preview.setHtml(html_content)
except Exception:
# 如果渲染失败,使用空HTML模板
self.markdown_preview.setHtml(self._get_empty_html("渲染失败,请查看Markdown源码"))
elif hasattr(self, 'markdown_preview'):
# 如果没有结果,更新空HTML模板
self.markdown_preview.setHtml(self._get_empty_html())
def _get_empty_html(self, message: str = "等待爬取结果...") -> str:
"""获取空HTML模板"""
bg_color = "#1e1e1e" if self.is_dark_mode else "#ffffff"
text_color = "#e0e0e0" if self.is_dark_mode else "#666"
return f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', Arial, sans-serif;
padding: 40px;
color: {text_color};
background-color: {bg_color};
text-align: center;
}}
</style>
</head>
<body>
<p>{message}</p>
</body>
</html>
"""
def _render_markdown(self, markdown_content: str) -> str:
"""将Markdown内容渲染为HTML"""
# 配置Markdown扩展
extensions = [
'codehilite', # 代码高亮
'tables', # 表格支持
'fenced_code', # 代码块支持
'nl2br', # 换行支持
'sane_lists', # 列表支持
]
# 转换Markdown为HTML
html_body = markdown.markdown(
markdown_content,
extensions=extensions,
extension_configs={
'codehilite': {
'css_class': 'highlight',
'use_pygments': False, # 不使用Pygments,使用简单样式
}
}
)
# 根据主题模式选择样式
if self.is_dark_mode:
# 暗色主题样式
bg_color = "#1e1e1e"
text_color = "#e0e0e0"
heading_color = "#ffffff"
border_color = "#404040"
code_bg = "rgba(255, 255, 255, 0.1)"
pre_bg = "#2d2d2d"
table_bg = "#2d2d2d"
table_border = "#404040"
link_color = "#66bb6a"
blockquote_color = "#b0b0b0"
blockquote_border = "#404040"
hr_color = "#404040"
else:
# 亮色主题样式
bg_color = "#ffffff"
text_color = "#333"
heading_color = "#24292e"
border_color = "#eaecef"
code_bg = "rgba(27, 31, 35, 0.05)"
pre_bg = "#f6f8fa"
table_bg = "#f6f8fa"
table_border = "#dfe2e5"
link_color = "#0366d6"
blockquote_color = "#6a737d"
blockquote_border = "#dfe2e5"
hr_color = "#e1e4e8"
# 创建完整的HTML文档,包含现代化样式
html_template = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', 'PingFang SC', Arial, sans-serif;
line-height: 1.6;
color: {text_color};
background-color: {bg_color};
padding: 40px;
max-width: 1200px;
margin: 0 auto;
}}
h1, h2, h3, h4, h5, h6 {{
margin-top: 24px;
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
color: {heading_color};
}}
h1 {{
font-size: 2em;
border-bottom: 1px solid {border_color};
padding-bottom: 0.3em;
}}
h2 {{
font-size: 1.5em;
border-bottom: 1px solid {border_color};
padding-bottom: 0.3em;
}}
h3 {{
font-size: 1.25em;
}}
p {{
margin-bottom: 16px;
}}
a {{
color: {link_color};
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
ul, ol {{
margin-bottom: 16px;
padding-left: 2em;
}}
li {{
margin-bottom: 4px;
}}
code {{
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
background-color: {code_bg};
border-radius: 3px;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
color: {text_color};
}}
pre {{
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: {pre_bg};
border-radius: 6px;
margin-bottom: 16px;
}}
pre code {{
display: inline;
padding: 0;
margin: 0;
overflow: visible;
line-height: inherit;
word-wrap: normal;
background-color: transparent;
border: 0;
}}
blockquote {{
padding: 0 1em;
color: {blockquote_color};
border-left: 0.25em solid {blockquote_border};
margin-bottom: 16px;
}}
table {{
border-spacing: 0;
border-collapse: collapse;
margin-bottom: 16px;
width: 100%;
}}
table th, table td {{
padding: 6px 13px;
border: 1px solid {table_border};
}}
table th {{
font-weight: 600;
background-color: {table_bg};
}}
table tr:nth-child(2n) {{
background-color: {table_bg};
}}
img {{
max-width: 100%;
height: auto;
border-radius: 4px;
margin: 16px 0;
}}
hr {{
height: 0.25em;
padding: 0;
margin: 24px 0;
background-color: {hr_color};
border: 0;
}}
.highlight {{
background-color: {pre_bg};
border-radius: 6px;
padding: 16px;
margin-bottom: 16px;
overflow-x: auto;
}}
</style>
</head>
<body>
{html_body}
</body>
</html>
"""
return html_template
def _on_deep_crawl_toggled(self, checked):
"""深度爬取选项切换"""
self.deep_crawl_options.setVisible(checked)
if not DEEP_CRAWL_AVAILABLE and checked:
QMessageBox.warning(
self,
"功能不可用",
"深度爬取功能需要安装crawl4ai的深度爬取模块。\n"
"请确保您的crawl4ai版本支持深度爬取功能。"
)
self.enable_deep_crawl_checkbox.setChecked(False)
self.deep_crawl_options.setVisible(False)
def _build_deep_crawl_strategy(self):
"""构建深度爬取策略"""
if not DEEP_CRAWL_AVAILABLE or not self.enable_deep_crawl_checkbox.isChecked():
return None
max_depth = self.max_depth_spinbox.value()
include_external = self.include_external_checkbox.isChecked()
max_pages = self.max_pages_spinbox.value() if self.max_pages_spinbox.value() < 1000 else None
# 构建过滤器链
filters = []
# URL模式过滤器
url_patterns = [p.strip() for p in self.url_pattern_input.text().split(",") if p.strip()]
if url_patterns:
filters.append(URLPatternFilter(patterns=url_patterns))
# 域名过滤器
allowed_domains = [d.strip() for d in self.allowed_domains_input.text().split(",") if d.strip()]
blocked_domains = [d.strip() for d in self.blocked_domains_input.text().split(",") if d.strip()]
if allowed_domains or blocked_domains:
filters.append(DomainFilter(
allowed_domains=allowed_domains if allowed_domains else None,
blocked_domains=blocked_domains if blocked_domains else None