-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
238 lines (196 loc) · 10.3 KB
/
main.py
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
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QSizePolicy, QLineEdit, QMessageBox, QTextBrowser, QFileDialog, QDialog
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QFont
from bs4 import BeautifulSoup
import requests
import sqlite3
import os
import mimetypes
os.environ["QT_LOGGING_RULES"] = "*.warn=false"
from about import AboutDialog
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Web Scraper Application")
self.setWindowIcon(QIcon('icon.png'))
self.setGeometry(100, 100, 800, 600) # Set window position and size
self.setStyleSheet("background-color: #b9b0b0;") # Set background color of main window
# Central widget and layout
central_widget = QWidget()
main_layout = QVBoxLayout(central_widget)
self.setCentralWidget(central_widget)
# Title label and toggle button layout
title_layout = QHBoxLayout()
main_layout.addLayout(title_layout)
# Title label
self.title_label = QLabel("Web Scraper Application", self)
gradient_color = "qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(90, 25, 155, 0.25), stop:1 rgba(0, 32, 240, 0.25));"
self.title_label.setStyleSheet("color: black; font-size: 24px; padding: 10px; background-color: " + gradient_color + " font-weight: bold; font-family: Arial, sans-serif;")
title_layout.addWidget(self.title_label)
# Button to toggle the task bar
self.toggle_button = QPushButton("MENU")
self.toggle_button.setIcon(QIcon("menu.png"))
self.toggle_button.setStyleSheet("QPushButton {""background-color: #7289da; ""color: #fff; ""font-weight: bold; ""font-size: 18px; ""}""QPushButton:hover {""background-color: #4e6eff; ""}")
self.toggle_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.toggle_button.clicked.connect(self.toggle_task_bar)
title_layout.addWidget(self.toggle_button)
# Initialize task bar
self.init_task_bar(main_layout)
# Initialize home page components
self.init_home_page(main_layout)
# Initialize scrape page components
self.init_scrape_page(main_layout)
# Initialize view page components
self.init_view_page(main_layout)
# Initially hide task bar, scrape page, and view page components
self.task_bar.hide()
self.scrape_page.hide()
self.view_page.hide()
def init_task_bar(self, layout):
# Sliding task bar
self.task_bar = QWidget()
self.task_bar.setStyleSheet("background-color: #23272a;") # Set background color
self.task_bar.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
self.task_bar_layout = QVBoxLayout(self.task_bar)
# Additional buttons on the task bar
button_names = [("HOME", "home.png"), ("SCRAPE", "scrape.png"), ("VIEW", "view.png"), ("ABOUT", "about.png"), ("QUIT","quit.png")] # Add more button names as needed
for name, icon_filename in button_names:
button = QPushButton(name)
button.setStyleSheet("QPushButton {"
"background-color: #7289da; "
"color: #fff; "
"font-weight: bold; "
"font-size: 18px; "
"text-align: left; "
"}"
"QPushButton:hover {"
"background-color: #4e6eff; "
"}")
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
if name == "QUIT":
button.clicked.connect(self.quit_application)
elif name == "ABOUT":
button.clicked.connect(self.show_about_dialog)
else:
button.clicked.connect(lambda checked, name=name: self.change_page(name))
# Set icon for the button
icon = QIcon(icon_filename)
button.setIcon(icon)
self.task_bar_layout.addWidget(button)
layout.addWidget(self.task_bar)
def toggle_task_bar(self):
if self.task_bar.isHidden():
self.task_bar.show()
else:
self.task_bar.hide()
def change_page(self, page_name):
self.setWindowTitle(page_name) # Change window title to reflect current page
# Show or hide home, scrape, and view page components based on button click
if page_name == "HOME":
self.home_page.show()
self.scrape_page.hide()
self.view_page.hide()
elif page_name == "SCRAPE":
self.home_page.hide()
self.scrape_page.show()
self.view_page.hide()
elif page_name == "VIEW":
self.home_page.hide()
self.scrape_page.hide()
self.view_page.show()
else:
self.home_page.hide()
self.scrape_page.hide()
self.view_page.hide()
def quit_application(self):
QApplication.quit()
def init_home_page(self, layout):
# Initialize home page components
self.home_page = QWidget()
home_layout = QVBoxLayout(self.home_page)
home_layout.setAlignment(Qt.AlignCenter)
home_image_label = QLabel(self.home_page)
pixmap = QPixmap("WS.png") # Replace "your_image.png" with the filename of your PNG image
home_image_label.setPixmap(pixmap)
home_layout.addWidget(home_image_label , alignment=Qt.AlignCenter)
home_text_label = QLabel("\n\t Introducing WebWeaver - your digital arachnid companion in the vast online jungle! \n\t\t WebWeaver scours the web, capturing valuable data. \n So say goodbye to manual data collection and hello to WebWeaver, your trusty web-scraping sidekick!\n", self.home_page)
home_text_label.setFont(QFont('New courier',10))
home_text_label.setStyleSheet("color: black; font-size: 24px; font-weight: bold; background-color: #CBCBCB; border: 1px solid;")
home_layout.addWidget(home_text_label)
layout.addWidget(self.home_page)
def init_scrape_page(self, layout):
# Initialize scrape page components
self.scrape_page = QWidget()
scrape_layout = QVBoxLayout(self.scrape_page)
scrape_layout.setAlignment(Qt.AlignCenter)
self.url_line_edit = QLineEdit(self.scrape_page)
self.url_line_edit.setPlaceholderText("Enter URL to scrape")
self.url_line_edit.setStyleSheet("color: black; font-weight: bold; font-size: 18px;")
scrape_layout.addWidget(self.url_line_edit)
scrape_button = QPushButton("Scrape", self.scrape_page)
scrape_button.setStyleSheet("QPushButton {""background-color: #7289da; ""color: white; ""font-weight: bold; ""font-size: 18px; ""}""QPushButton:hover {""background-color: #4e6eff; ""}")
scrape_button.clicked.connect(self.scrape_website)
scrape_layout.addWidget(scrape_button)
layout.addWidget(self.scrape_page)
def show_about_dialog(self):
dialog = AboutDialog()
dialog.exec_()
def scrape_website(self):
url = self.url_line_edit.text().strip()
if not url:
QMessageBox.warning(self, "Error", "Please enter a valid URL.")
return
try:
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
# Display HTML content in the view page
html_content = str(soup)
self.view_content_browser.setPlainText(html_content)
# Switch to view page
self.change_page("VIEW")
# Save HTML content to text file
self.save_html_content(html_content)
else:
QMessageBox.warning(self, "Error", "Failed to retrieve webpage. Please check the URL.")
except Exception as e:
QMessageBox.warning(self, "Error", f"An error occurred: {str(e)}")
def save_html_content(self, content):
try:
with open("scraped_content.html", "w", encoding="utf-8") as file:
file.write(content)
QMessageBox.information(self, "Success", "HTML content saved successfully!")
except Exception as e:
QMessageBox.warning(self, "Error", f"Failed to save HTML content: {str(e)}")
def init_view_page(self, layout):
# Initialize view page components
self.view_page = QWidget()
view_layout = QVBoxLayout(self.view_page)
view_layout.setAlignment(Qt.AlignCenter)
view_label = QLabel("HTML CONTENT", self.view_page)
view_label.setStyleSheet("color: black; font-size: 24px; font-weight: bold;")
view_layout.addWidget(view_label)
self.view_content_browser = QTextBrowser(self.view_page)
self.view_content_browser.setStyleSheet("color: white; background-color: black;")
view_layout.addWidget(self.view_content_browser)
# Button to select previously scraped websites
select_button = QPushButton("Select Previous Scrapes", self.view_page)
select_button.setStyleSheet("QPushButton {""background-color: #7289da; ""color: white; ""font-weight: bold; ""font-size: 18px; ""}""QPushButton:hover {""background-color: #4e6eff; ""}")
select_button.clicked.connect(self.select_previous_scrapes)
view_layout.addWidget(select_button)
layout.addWidget(self.view_page)
def select_previous_scrapes(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_name, _ = QFileDialog.getOpenFileName(self, "Select Text File", "", "Text Files (*.txt)", options=options)
if file_name:
with open(file_name, "r", encoding="utf-8") as file:
content = file.read()
self.view_content_browser.setPlainText(content)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()