[ad_1]
Python breaks down when I click “Buscar” (“Search” in English) and I cannot find why. I would also like help in making links clickable and opening web pages, like other browsers do.
One of the problems is “Cannot find reference ‘connect’ in ‘function'” and I guess the main problems should lie somewhere there.
The code is below:
from bs4 import BeautifulSoup
import requests
import json
from deep_translator import GoogleTranslator
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(60, 30, 401, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setText("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(480, 30, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.do_action)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 90, 681, 81))
self.label.setText("")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 180, 681, 61))
self.label_2.setText("")
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 250, 681, 61))
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 320, 681, 61))
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(30, 390, 681, 61))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.verticalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.verticalScrollBar.setGeometry(QtCore.QRect(780, 0, 20, 651))
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setObjectName("verticalScrollBar")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(30, 460, 681, 61))
self.label_6.setText("")
self.label_6.setObjectName("label_6")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def do_action(self):
a = self.lineEdit.text()
global params
params = [a]
translation = GoogleTranslator(source="spanish", target="english").translate(text=a)
translation = translation.replace(" ", "+")
translation = translation.replace("'", "%27")
translation = translation.replace("!", "%21")
translation = translation.replace("#", "%23")
translation = translation.replace("$", "%24")
translation = translation.replace("%", "%25")
translation = translation.replace("&", "%26")
translation = translation.replace("(", "%28")
translation = translation.replace(")", "%29")
translation = translation.replace("*", "%2a")
translation = translation.replace("/", "%2f")
translation = translation.replace(",", "%2c")
translation = translation.replace(":", "%3a")
translation = translation.replace(";", "%3b")
translation = translation.replace("<", "%3c")
translation = translation.replace("=", "%3d")
translation = translation.replace(">", "%3e")
translation = translation.replace("?", "%3f")
translation = translation.replace("@", "%40")
translation = translation.replace("[", "%5b")
translation = translation.replace("]", "%5d")
translation = translation.replace("^", "%5e")
translation = translation.replace("_", "%5f")
translation = translation.replace("{", "%7b")
translation = translation.replace("|", "%7c")
translation = translation.replace("}", "%7d")
translation = translation.replace("~", "%7e")
translation = translation.replace("€", "%80")
translation = translation.replace("ƒ", "%83")
translation = translation.replace("…", "%85")
headers = {
'User-agent':
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}
html = requests.get('https://www.google.com/search?q=' + translation + "&oq=" + translation + "&aqs=chrome",
headers=headers)
soup = BeautifulSoup(html.text, 'lxml')
data = []
for result in soup.select('.tF2Cxc'):
title = result.select_one('.DKV0Md').text
link = result.select_one('.yuRUbf a')['href']
snippet = result.select_one('#rso .lyLwlc')
data.append({
'title': title,
'link': link,
'snippet': snippet.text,
})
hru = json.dumps(data)
res = json.loads(hru)
hru1 = res[0]
hru2 = res[1]
hru3 = res[2]
hru4 = res[3]
hru5 = res[4]
h = list(hru1.values())
translation0 = GoogleTranslator(source="english", target="spanish").translate(text=h[0])
translation1 = GoogleTranslator(source="english", target="spanish").translate(text=h[2])
hm = [translation0, h[1], translation1]
t = list(hru2.values())
translation2 = GoogleTranslator(source="english", target="spanish").translate(text=t[0])
translation3 = GoogleTranslator(source="english", target="spanish").translate(text=t[2])
tm = [translation2, t[1], translation3]
u = list(hru3.values())
translation4 = GoogleTranslator(source="english", target="spanish").translate(text=u[0])
translation5 = GoogleTranslator(source="english", target="spanish").translate(text=u[2])
um = [translation4, u[1], translation5]
o = list(hru4.values())
translation6 = GoogleTranslator(source="english", target="spanish").translate(text=o[0])
translation7 = GoogleTranslator(source="english", target="spanish").translate(text=o[2])
om = [translation6, o[1], translation7]
p = list(hru5.values())
translation8 = GoogleTranslator(source="english", target="spanish").translate(text=p[0])
translation9 = GoogleTranslator(source="english", target="spanish").translate(text=p[0])
pm = [translation8, p[1], translation9]
self.label.setText(hm[0] + "n"+ hm[1] + "n"+ hm[2])
self.label.adjustSize()
self.label_2.setText(tm[0] + "n"+ tm[1] + "n"+ tm[2])
self.label_2.adjustSize()
self.label_3.setText(um[0] + "n"+ um[1] + "n"+ um[2])
self.label_3.adjustSize()
self.label_4.setText(om[0] + "n"+ om[1] + "n"+ om[2])
self.label_4.adjustSize()
self.label_5.setText(pm[0] + "n"+ pm[1] + "n"+ pm[2])
self.label_5.adjustSize()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Buscar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
[ad_2]