Spaces:
Sleeping

Login / Pyqt5.py
Pezh's picture
Create Pyqt5.py
76a7948 verified
Raw History Blame Contribute Delete
1.97 kB
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit, QVBoxLayout, QWidget
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
class WikiScraper(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Create a textbox
self.textBox = QLineEdit(self)
self.textBox.setPlaceholderText("Type search term here...")
# Create a button in the window
self.button = QPushButton('Go to wiki', self)
self.button.clicked.connect(self.on_click)
# set the layout
layout = QVBoxLayout()
layout.addWidget(self.textBox)
layout.addWidget(self.button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.setWindowTitle('Wiki Searcher')
self.setGeometry(300, 300, 350, 200)
def on_click(self):
searchTerm = self.textBox.text()
if searchTerm:
driver.get("https://www.wikipedia.org/")
searchBox = driver.find_element(By.NAME, "search")
searchBox.clear()
searchBox.send_keys(searchTerm)
searchBox.send_keys(Keys.RETURN)
# Now the search is done, you could continue to scrape results if needed
# Here an example of copying first heading from the results
firstHeading = driver.find_element(By.ID, "firstHeading")
self.textBox.setText(firstHeading.text)
if __name__ == '__main__':
# Set up the Selenium driver for Chrome
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver_path = '/path/to/chromedriver' # Update this to your Chromedriver path
driver = webdriver.Chrome(driver_path, options=options)
app = QApplication(sys.argv)
ex = WikiScraper()
ex.show()
sys.exit(app.exec_())