某站动画列表

PyQt5与爬虫(一)——爬取某站动画每周列表

PyQt程序截图,可以点击图片按钮,然后会打开谷歌浏览器到你选择的动漫界面。

PyQt5与爬虫(一)——爬取某站动画每周列表

PyQt5与爬虫(一)——爬取某站动画每周列表

贴代码:

main.py

from PyQt5.QtWidgets import QWidget,QApplication
import sys
from MyWidget import Widget
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

MyWidegt.py

from PyQt5.QtWidgets import QWidget,QTabWidget,QHBoxLayout
from TabWidget import Tab
from  bs4 import BeautifulSoup
import requests
import pathlib
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Widget(QWidget):
    def __init__(self):
        super().__init__()

        self.resize(800,450)
        self.tabWidget = QTabWidget(self)

        hlay = QHBoxLayout(self)
        hlay.addWidget(self.tabWidget)
        self.setLayout(hlay)

        L = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
        self.tab1 = Tab()
        self.tab2 = Tab()
        self.tab3 = Tab()
        self.tab4 = Tab()
        self.tab5 = Tab()
        self.tab6 = Tab()
        self.tab7 = Tab()


        self.tabWidget.addTab(self.tab1, L[0])
        self.tabWidget.addTab(self.tab2, L[1])
        self.tabWidget.addTab(self.tab3, L[2])
        self.tabWidget.addTab(self.tab4, L[3])
        self.tabWidget.addTab(self.tab5, L[4])
        self.tabWidget.addTab(self.tab6, L[5])
        self.tabWidget.addTab(self.tab7, L[6])


        # self.tabWidget.currentChanged[int].connect(self.recvChanged)

        self.url = "http://www.dilidili.wang/"
        html = requests.get(self.url).content.decode('utf-8')
        soup = BeautifulSoup(html, 'lxml')
        self.rets = soup.select("ul.wrp.animate")
        for i in range(7):
            self.recvChanged(i)

    def parseHtml(self,html,index):
        tooltips = html.select('a.tooltip')
        for tooltip in tooltips:
            href = tooltip['href']
            src = tooltip.select('img')[0]['src']
            # name = tooltip.select('p')[0].get_text()
            # name = ""
            num = str("")
            span = str("")
            t = tooltip.select('span')
            if len(t) > 0:
                span = t[0].get_text()
            if len(tooltip.select('p')) > 1:
                name = tooltip.select('p')[0].get_text()
                num = tooltip.select('p')[1].get_text()
            else:
                name = tooltip.select('p')[0].get_text()
            # print(self.url +href, self.url + src, name, num, span)
            pixture = name+'.jpg'
            path = pathlib.Path(pixture)
            if not path.exists():
                print(path)
                tupian = requests.get(self.url+src).content
                with open(pixture,'wb') as f:
                    f.write(tupian)
                f.close()

            if index == 0:
                self.tab1.setParams(self.url+href,pixture,name,num,span)
            elif index == 1:
                self.tab2.setParams(self.url + href, pixture, name, num, span)
            elif index == 2:
                self.tab3.setParams(self.url + href, pixture, name, num, span)
            elif index == 3:
                self.tab4.setParams(self.url + href, pixture, name, num, span)
            elif index == 4:
                self.tab5.setParams(self.url + href, pixture, name, num, span)
            elif index == 5:
                self.tab6.setParams(self.url + href, pixture, name, num, span)
            elif index == 6:
                self.tab7.setParams(self.url + href, pixture, name, num, span)

        pass
    def recvChanged(self,index):

        # tools =  self.tabWidget.findChildren(QToolButton,'', Qt.FindChildrenRecursively)
        # print(index)
        if index == 0:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-one > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 1:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-two > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 2:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-three > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 3:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-four > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 4:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-five > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 5:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-six > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)
        elif index == 6:
            for ret in self.rets:
                htmls = ret.select("li.elmnt-seven > div.book.small")
                for html in htmls:
                    self.parseHtml(html,index)



TabWidget.py

from PyQt5.QtWidgets import QWidget,QTabWidget,QHBoxLayout,QVBoxLayout,QGridLayout,QPushButton,QToolButton
from PyQt5.QtGui import QIcon,QPixmap
from PyQt5.QtCore import *
from MyBtn import MyButton
from PyQt5.QtNetwork import QNetworkRequest

class Tab(QWidget):
    def __init__(self):
        super().__init__()
        self.gridLay = QGridLayout()

        self.gridLay.setAlignment(Qt.AlignCenter)
        self.setLayout(self.gridLay)
        self.gridLay.setSpacing(0)
        self.gridLay.setAlignment(Qt.AlignTop)
        self.count = 0

    def setParams(self,href,src,name,num,span):
        row = self.count / 4
        col = self.count % 4
        self.count += 1
        btn = MyButton()
        # btn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.gridLay.addWidget(btn,row,col)
        # btn.setIconSize(QSize(50,50))
        btn.setBtnIcon(QIcon(src))
        btn.setLabelText(name + "\n" + num)
        btn.setHref(href)
        btn.setNewSpan(span)
        #传递额外参数,这个有点好用啊
        # btn.clicked.connect(lambda : self.onClicked(href))
    # def onClicked(self,href):
    #
    #     wd = webdriver.Chrome()
    #     wd.get(href)
    #     pass

MyBtn.py

from PyQt5.QtWidgets import QWidget,QVBoxLayout,QLabel,QPushButton
from selenium import webdriver
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MyButton(QWidget):
    def __init__(self):
        super().__init__()
        vbox = QVBoxLayout(self)
        self.btn = QPushButton(self)
        self.label = QLabel(self)
        self.label.setAlignment(Qt.AlignCenter)
        self.btn.clicked.connect(self.onClicked)
        self.btn.setFixedSize(200,110)
        self.btn.setIconSize(QSize(200,110))
        self.btn.setAutoFillBackground(True)
        # self.btn.setFlat(True)
        vbox.addWidget(self.btn)
        vbox.addWidget(self.label)
        self.href = str()
        self.setLayout(vbox)
        self.span = str()
    def setBtnIcon(self,pix):
        self.btn.setIcon(pix)
    def setLabelText(self,str):
        self.label.setText(str)
    def setHref(self,href):
        self.href = href
    def onClicked(self):
        wd = webdriver.Chrome()
        wd.get(self.href)
    def setNewSpan(self,sp):
        self.span = sp
        self.update()
    def paintEvent(self, QPaintEvent):
        painter = QPainter(self)
        painter.setPen(QPen(Qt.red))
        painter.drawText(self.rect(),Qt.AlignRight,self.span)
        pass

相关文章: