【Python简单爬虫设计】对豆瓣TOP100的电影名及简要的爬取

1.使用Designer创建图形界面(详细操作见往期博客点击打开链接

对UI控件的绑定代码片段

def __init__(self):
    QtGui.QMainWindow.__init__(self)
    pachong.Ui_MainWindow.__init__(self)
    self.setupUi(self)
    self.pushButton.clicked.connect(self.ButtonFunction1)  # 开始爬取
    self.pushButton_2.clicked.connect(self.ButtonFunction2)  # 第二层分析预览
    self.pushButton_4.clicked.connect(self.ButtonFunction4)  # 第二层分析保存
    self.pushButton_6.clicked.connect(self.ButtonFunction6)  # 第一层分析预览
    self.pushButton_7.clicked.connect(self.ButtonFunction7)  # 第一层分析保存

2. 代码解析-开始爬取

 def ButtonFunction1(self):
        web = str(self.lineEdit.text())
        res = requests.get(web)
        sou = BeautifulSoup(res.text, 'html.parser')
        global sou
        for c in sou.select('div.hd > a'):
           b = c['href']
           res = requests.get(b)
           soup = BeautifulSoup(res.text, 'html.parser')
           global soup
           for e in soup.find_all('span',property='v:summary'):
              data= e.get_text()
              global  data
        self.textEdit.append('Download Webdata successfully !')
获取网站数据后显示爬取成功!

3.一层分析-爬取电影名字&保存

    def ButtonFunction6(self):     # 第一层分析预览
        self.textEdit.append('use data......')
        web = str(self.lineEdit.text())
        res = requests.get(web)
        soup = BeautifulSoup(res.text, 'html.parser')
        dat = soup.find_all('span', class_="title")
        global  dat
        for n in dat:
            sta = n.get_text()
            self.textEdit.append(sta)
    def ButtonFunction7(self):    # 第一层分析保存
        f = open('E:\Destop\movietitle.txt', 'wb')
        for n in dat:
            sta = n.get_text()
            f.write(sta)
        f.close()
        self.textEdit.append('save successfully !')

保存到本地的txt文件:


4.二层分析-爬取电影简介&保存

    def ButtonFunction2(self):      # 第二层分析预览
        for c in sou.select('div.hd > a'):
         for e in soup.find_all('span', property='v:summary'):
              self.textEdit.append(data)
    def ButtonFunction4(self):     # 第二层分析保存
        f = open('E:\Destop\movie.txt', 'wb')
        for c in sou.select('div.hd > a'):
            for e in soup.find_all('span', property='v:summary'):
              f.write(data)
        f.close()
        self.textEdit.append('save successfully !')

保存到本地txt文件预览:


5.Main 程序

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())


猜你喜欢

转载自blog.csdn.net/keylion_/article/details/79424158