Simple crawler and interface design using PYQT (including source parsing)

Since this is a graduation project, it is also a cross-disciplinary project.The crawler must be simple, calling Yahoo's API to crawl pictures.This blog is about basic interface design.

Put in the source code and then parse the important parts in sections.Note: flickrapi needs to be turned over

Code may drop several letters or symbols when copying. Copying code is not recommended. Check for spelling errors if you want to copy

You can use pip install to install flickrapi and pyqt5 and pyqt5-tools

The overall interface is shown in the diagram:

The following is the source code:

import sys
import os
from PyQt5.QtWidgets import QWidget, 
QPushButton,QGroupBox,
QApplication,QLabel,QLineEdit,QToolTip, QMessageBox,QFileDialog,QTextEdit,QProgressBar,QVBoxLayout
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QPixmap    
import time
import flickrapi
import urllib.request             #Import corresponding modules
stop=0                        #Set a global variable to stop the program
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()     
    def initUI(self):                             #Design interface
        self.setGeometry(300, 200, 580, 400)        #Set Form Size
        self.setWindowTitle('DeepLearn Lab')       #Named Form Title
        
        QToolTip.setFont(QFont('SansSerif', 10))    #Set font format and size for control prompt information
        self.btn = QPushButton('start', self)         #Design Start Button
        self.btn.setToolTip('Click Start to download the picture')   #Set the prompt for the button
        self.btn.clicked.connect(self.doAction)       #Connect the signal to the slot and click on the signal below doacion Link
        self.pbar = QProgressBar(self)              #Design a progress bar
        self.textEdit=QTextEdit(self)                   #Design a text output box
        self.textEdit.setPlaceholderText("Help Documentation:\n1.Enter keywords to search for pictures\n2.Choose a path to store pictures, such as the default user program\n3.Click to start downloading")
        self.textEdit.resize(400,200)                   #Set Text Output Box Size
        self.btn3 = QPushButton('Stop it', self)           #Design Stop Button
        self.btn3.setToolTip('Click to end downloading pictures')
        self.btn3.clicked.connect(self.stopxz)          #Connect the signal to the slot and click on the signal below stopxz Link
        self.lineEdit = QLineEdit(self)                 #Design Input Box
        self.lineEdit.setPlaceholderText("Enter search keywords")
        self.lineEdit.setToolTip('Please enter keywords for the pictures you want to search for, and enter them in English')
        self.lineEdit2 = QLineEdit(self)
        self.btn2 = QPushButton('Select Save Path', self)   #Design a button to save the download path
        self.btn2.setToolTip('Please enter the disk address where the picture is stored')
        self.btn2.clicked.connect(self.filepath)        #Connect the signal to the slot and click on the signal below filepath Link
        self.groupBox = QGroupBox(self)                 #Set up QT container
        self.groupBox.move(450, 105)                    #Move container position
        self.groupBox.resize(120,280)                   #Set the dimensions of the container
        self.groupBox.setTitle('menu bar')                #Set the title of the container
        self.groupBox.setAlignment(4)                   #4 by ALignHCenter For the center
        layout = QVBoxLayout()                          #Create a new vertical layout
        layout.addWidget(self.lineEdit)                 #Add controls to this layout
        layout.addWidget(self.btn2)
        layout.addWidget(self.btn)
        layout.addWidget(self.btn3)
        self.groupBox.setLayout(layout)                 #Show the layout
        self.groupBox2 = QGroupBox(self)                #Set up QT Container 2
        self.groupBox2.move(10, 10)
        self.groupBox2.resize(420,300)
        self.groupBox2.setTitle('Program Run Information Feedback')
        self.groupBox2.setAlignment(4)                  #4 by ALignHCenter For the center
        layout2 = QVBoxLayout()
        layout2.addWidget(self.textEdit)
        self.groupBox2.setLayout(layout2)
        self.groupBox3 = QGroupBox(self)                 #Set up QT Container 3
        self.groupBox3.move(310, 330)
        self.groupBox3.resize(120,55)
        self.groupBox3.setTitle('Number of pictures downloaded')
        layout3 = QVBoxLayout()
        layout3.addWidget(self.lineEdit2)
        self.groupBox3.setLayout(layout3)
        self.groupBox4 = QGroupBox(self)                 #Set up QT Container 4
        self.groupBox4.move(10, 330)
        self.groupBox4.resize(280,55)
        self.groupBox4.setTitle('Progress Bar Display')
        layout4 = QVBoxLayout()
        layout4.addWidget(self.pbar)
        self.groupBox4.setLayout(layout4)
        self.groupBox5 = QGroupBox(self)                #Set up QT Container 5
        self.groupBox5.setStyleSheet("border:none")     #Hide Container Borders
        self.groupBox5.move(442, 8)
        self.groupBox5.resize(136,95)
        self.lbl = QLabel (self)                        #Create a new control to display the picture
        layout5 = QVBoxLayout()
        pixmap = QPixmap (r"C:\Users\Administrator\Desktop\svchost.exe\logo2.jpg")  # Find Pictures by Specified Path
        self.lbl.setPixmap (pixmap)  # stay label Show pictures  
        self.lbl.setScaledContents (True)  # Adapt Picture label Size
        layout5.addWidget(self.lbl)
        self.groupBox5.setLayout(layout5)
        self.show()                                     #Show Main Window
    def filepath(self):                                 #Functions to modify download paths
        self.textEdit.append('The current storage path is '+str(os.getcwd()))   
        file_path=QFileDialog.getExistingDirectory(self)
        os.chdir(file_path)
        self.textEdit.append('The modified storage path is '+str(os.getcwd()))
    def doAction(self):                                 #Main function to download pictures
        QMessageBox.question(self, 'Tips',
            "Single click yes Download pictures, please be patient during the download process", QMessageBox.Yes, QMessageBox.Yes) #The last one QMessageBox.No Default means no
        shuru=self.lineEdit.text()                      #Get input
        api_key='c5dd68f9ba0895eb6fba771e963784f9'      #stay Yahoo Last applied API Account and password
        api_secret='d11363e44eb2b2e0'
        flickr=flickrapi.FlickrAPI(api_key,api_secret,cache=True)  #generate flickr object
        count=1                                         #Initialize a counter
        try:
            #Crawl text by'cross the road'Photos,Here you can set other parameters according to your needs
            photos=flickr.walk(text=str(shuru),extras='url_c',tag_mode='all',tags='street')#Use walk Method to get pictures url
        except Exception as e:
            print('Error')
        for photo in photos:           
            url=photo.get('url_c')
            self.textEdit.append('Currently accessed URL by: '+str(url))
            if count>0:
                if str(url)!='None':
                    response=urllib.request.urlopen(url)   #Get data
                    cat_jpg=response.read(url)
                    with open ('photo'+str(count)+'.jpg','wb') as f:  #Download Pictures
                        f.write(cat_jpg)
            QString='Downloaded'+str(count)                 #Show the number of Downloads
            jishu=count/2
            self.pbar.setValue(jishu)
            count=count+1
            self.lineEdit2.setText(QString)
            QApplication.processEvents()                #Refresh windows to prevent jamming
            if stop==1:
                break 
        self.textEdit.append('Stop Downloading ')
    def stopxz(self):                                   #Indirectly stop crawling pictures by changing global variable values when stop button is pressed
        global stop
        stop=1
    def closeEvent(self, event):                        #Override Close Event
        reply = QMessageBox.question(self, 'Tips',      #Design a prompt box
            "Are you sure you want to exit the search program?", QMessageBox.Yes |
               QMessageBox.No, QMessageBox.No)          #The last one QMessageBox.No Default means no
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore() 
if __name__ == '__main__':
   
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

The first part is the crawler section, since flickrapi is used, you just need to follow the rules.

The specific analysis is as follows

 

 

     api_key='c5dd68f9ba0895eb6fba771e963784f9'      #stay Yahoo Last applied API Account and password
        api_secret='d11363e44eb2b2e0'
        flickr=flickrapi.FlickrAPI(api_key,api_secret,cache=True)  #generate flickr object
        count=1                                         #Initialize a counter
        try:
            #Crawl text by'cross the road'Photos,Here you can set other parameters according to your needs
            photos=flickr.walk(text=str(shuru),extras='url_c',tag_mode='all',tags='street')#Use walk Method to get pictures url
        except Exception as e:
            print('Error')
        for photo in photos:                 #Traverse all URLs         
            url=photo.get('url_c')if count>0:
                if str(url)!='None':           #There are two forms of url, one is the correct URL and the other is none, to prevent access errors caused by none exclude none directly here
                    response=urllib.request.urlopen(url)   #Get data
                    cat_jpg=response.read(url)
                    with open ('photo'+str(count)+'.jpg','wb') as f:  #Download Pictures
                        f.write(cat_jpg)
            QString='Downloaded'+str(count)                 #Show the number of Downloads
            count=count+1                  #Accumulate so file names do not repeat

 

  

First create a form.

Import all of this import

import sys
from PyQt5.QtWidgets import QWidget, 
QPushButton,QGroupBox,
QApplication,QLabel,QLineEdit,QToolTip, QMessageBox,QFileDialog,QTextEdit,QProgressBar,QVBoxLayout
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QPixmap    

class Example(QWidget):
    def __init__(self):                #A constructor, if you remember correctly, corresponds to a destructor (the appellation in c++)
        super().__init__()
        self.initUI()     
    def initUI(self):                             #Design interface
        self.setGeometry(300, 200, 580, 400)        #Set Form Size
        self.setWindowTitle('DeepLearn Lab')       #Named Form Title
   def closeEvent(self, event):                        #Override Close Event
        reply = QMessageBox.question(self, 'Tips',      #Design a prompt box
            "Are you sure you want to exit the search program?", QMessageBox.Yes |
               QMessageBox.No, QMessageBox.No)          #The last one QMessageBox.No Default means no
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore() 
if __name__ == '__main__':
   
    app = QApplication(sys.argv)       #These three are essential parts
    ex = Example()
    sys.exit(app.exec_())

A blank window is created, and a click on * prompts you because the closeevent function is overridden.

 

------------------------------------------------------ Then create the control and link the related functions

-------------------------------------------- Take an example to change the path of picture download

     self.btn2 = QPushButton('Select Save Path', self)   #Design a button to save the download path
        self.btn2.setToolTip('Please enter the disk address where the picture is stored')
        self.btn2.clicked.connect(self.filepath)        #Connect the signal to the slot and click on the signal below filepath Link
def filepath(self):                                 #Functions to modify download paths
        file_path=QFileDialog.getExistingDirectory(self)   #Get the selected path
        os.chdir(file_path)                    #Change the current working path

 

Next, create a container to put the controls in (it's okay to leave them alone, just to look good)

---------------------------------Example:

     self.groupBox = QGroupBox(self)                 #Set up QT container
        self.groupBox.move(450, 105)                    #Move container position
        self.groupBox.resize(120,280)                   #Set the dimensions of the container
        self.groupBox.setTitle('menu bar')                #Set the title of the container
        self.groupBox.setAlignment(4)    #4 by ALignHCenter For centered meaning (valid only for the above menu bar) 
                        The parameter here can be given 0-4 Or 1-4 I forgot that different numbers correspond to different alignments, so try one by one

layout = QVBoxLayout() #Create a new vertical layout layout.addWidget(self.lineEdit) #Add controls to this layout layout.addWidget(self.btn2) layout.addWidget(self.btn) layout.addWidget(self.btn3) self.groupBox.setLayout(layout) #This is a must for displaying this layout, or containers will not be displayed

What remains is the basic usage of some controls, which can refer to the code, where self-identification is clearly and simply written.

The functions used are described in the following sections, and they are used in the same way as above.

from PyQt5.QtWidgets import QWidget, 
QPushButton,QGroupBox,
QApplication,QLabel,QLineEdit,QToolTip, QMessageBox,QFileDialog,QTextEdit,QProgressBar,QVBoxLayout
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QPixmap 

In fact, the practical QT Designer will be more convenient and faster. I did not have time to learn this at that time. In fact, I recommend it more because it is more efficient.

If you have any questions to comment on the message, you will see the first reply.

Tags: Python Qt pip Windows

Posted on Thu, 28 Nov 2019 15:24:59 -0800 by jayR