Python3爬虫(sqlite3存储信息)–AGE动漫网站排行榜

目录

目标

1.爬虫代码

1.1运行结果

1.2爬虫难点

1.2.1编写正则表达式:

1.3爬虫中的不足

1.3.1抓取的动漫播放链接不够完整

2.GUI展现爬虫内容

2.1思路

2.2运行结果

2.3GUI设计难点

2.3.1按标题查询–模糊查询

爬取来源链接:https://m.agefans.org/rank

目标

爬取出排行榜1~100个动漫的排名、名称、热度的全部信息,将信息存储在sqlite3数据库 中,并且最终用GUI 展现出爬取出的结果。

1.爬虫代码

import requests
import re
import sqlite3

def get_page(url):#返回url的HTML源码
    response = requests.get(url)
    if response.status_code==200:
      return response.text
    else:
      return '失败'

def parse_html(html):#解析参数html的HTML源码
    #正则表达式匹配动漫排名,播放链接,名称,人气值
    pattern=re.compile('<li class="row mb-1 rank-item">.*?rank.*?>(\d+).*?href="(.*?)".*?age-text-blue small.*?>(.*?).*?col-3 small text-truncate.*?>(.*?)', re.S)
    result=re.findall(pattern, html)
    return result

def link_html():#&#x6293;&#x53D6;&#x6765;&#x6E90;html
    html = get_page('https://m.agefans.org/rank')
    result1 = parse_html(html)
    return result1

def save_db():#&#x5C06;&#x722C;&#x53D6;&#x5230;&#x7684;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5230;sqlite&#x4E2D;
    result1=link_html()
    con=sqlite3.connect(r'...\AGE.db')
    con.execute("""DROP TABLE data""")
    con.execute("create table data (rank primary key,link,title,rating)")
    cur=con.cursor()
    cur.executemany("insert into data(rank,link,title,rating) values(?,?,?,?)",result1)
    con.commit()
    cur.close()
    con.close()

if __name__ == '__main__':
    save_db()</li>

1.1运行结果

Python3爬虫(sqlite3存储信息)--AGE动漫网站排行榜

用DB Browser for SQLite查看AGE.db爬取的内容(展示1~35条信息)

1.2爬虫难点

1.2.1编写正则表达式:

排名:.?rank.?>(\d+)

链接:.?href=”(.?)”

名称:.?age-text-blue small.?>(.*?)

热度:.?col-3 small text-truncate.?>(.*?)

每个网站对应的页面元素组成各不相同,需要根据实际所需要爬取的网站写出对应的正则表达式。CTRL+SHIFT+I进入检查页面查看元素。

1.3爬虫中的不足

1.3.1抓取的动漫播放链接不够完整

链接link因为网站的

Python3爬虫(sqlite3存储信息)--AGE动漫网站排行榜

2.GUI展现爬虫内容

import tkinter
import tkinter.messagebox
from tkinter.messagebox import *
import tkinter.ttk
import tkinter as tk
import sqlite3
from PIL import ImageTk, Image
from tkinter import ttk
import pymysql
win=tkinter.Tk()

#&#x9875;&#x9762;&#x5927;&#x5C0F;
win.geometry("1390x750")
win.title('AGE&#x6392;&#x884C;&#x699C;')

#&#x6807;&#x9898;
label=tkinter.Label(win,compound = 'center',text='AGE&#x52A8;&#x6F2B;&#x6392;&#x884C;&#x699C;',font=('&#x9ED1;&#x4F53;',40),fg='#db7093',bg='#add8e6',width='500')
label.pack()

#&#x80CC;&#x666F;&#x56FE;&#x7247;
imgpath = (r'...\1.jpg')#&#x80CC;&#x666F;&#x56FE;&#x7247;&#x8DEF;&#x5F84;
img = Image.open(imgpath)
canvas = tk.Canvas(win, width=2500, height=1000, bd=0)
photo = ImageTk.PhotoImage(img)
canvas.create_image(690, 280, image=photo)
canvas.pack()

from tkinter import *
Label(win, text="&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#xFF1A;",bg='#add8e6',font=('&#x9ED1;&#x4F53;',15)).place(x=500, y=80, width=120, height=25)
selecttitle = StringVar()
Entry(win, textvariable=selecttitle).place(x=650, y=80, width=300, height=25)

&#x6570;&#x636E;&#x5E93;&#x4F4D;&#x7F6E;
database = (r'...\AGE.db')

&#x663E;&#x793A;&#x51FD;&#x6570;
def showAllInfo():
    # &#x5C06;&#x4E4B;&#x524D;&#x663E;&#x793A;&#x7684;&#x5185;&#x5BB9;&#x5220;&#x9664;
    x = dataTreeview.get_children()
    for item in x:
        dataTreeview.delete(item)
    # &#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;
    con = sqlite3.connect(database)
    cur = con.cursor()
    cur.execute("select * from data")
    lst = cur.fetchall()
    for item in lst:
        dataTreeview.insert("", 100, text="line1", values=item)
    cur.close()
    con.close()

#&#x6309;&#x6807;&#x9898;&#x67E5;&#x8BE2;
def showTitle():
   if selecttitle.get() == "":
       showerror(title='&#x63D0;&#x793A;', message='&#x8F93;&#x5165;&#x4E0D;&#x80FD;&#x4E3A;&#x7A7A;')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = sqlite3.connect(database)
       cur = con.cursor()
       content="'%"+selecttitle.get()+"%'"  #&#x8FDB;&#x884C;&#x6A21;&#x7CCA;&#x67E5;&#x8BE2;
       cur.execute("select * from data where title like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:  #&#x5224;&#x65AD;&#x5982;&#x679C;&#x67E5;&#x8BE2;&#x4E0D;&#x5230;&#x5219;&#x63D0;&#x793A;&#x67E5;&#x8BE2;&#x4E0D;&#x5230;&#x7A97;&#x53E3;
           showerror(title='&#x63D0;&#x793A;', message='&#x6B64;&#x52A8;&#x6F2B;&#x6682;&#x672A;&#x4E0A;&#x699C;&#xFF0C;&#x6216;&#x68C0;&#x67E5;&#x8F93;&#x5165;&#x4FE1;&#x606F;&#x662F;&#x5426;&#x6B63;&#x786E;')
       else:#&#x5426;&#x5219;&#x663E;&#x793A;&#x67E5;&#x8BE2;&#x51E0;&#x6761;&#x8BB0;&#x5F55;&#x7A97;&#x53E3;
           showinfo(title='&#x63D0;&#x793A;', message='&#x67E5;&#x8BE2;&#x5230;'+str(len(lst))+"&#x6761;&#x6570;&#x636E;")
           for item in lst:
               dataTreeview.insert("", 100, text="line1", values=item)
       cur.close()
       con.close()

tkinter.Button(win,text='&#x67E5;&#x8BE2;&#x5168;&#x90E8;',width=40,command=showAllInfo,font=(12)).place(x=800, y=125, width=120, height=30)
Button(win, text="&#x6309;&#x6807;&#x9898;&#x67E5;&#x8BE2;", command=showTitle,font=(12)).place(x=550, y=125, width=120, height=30)

#&#x5217;&#x8868;sqlite&#x6570;&#x636E;
dataTreeview = ttk.Treeview(win, show='headings', column=('rank','link', 'title', 'rating'))
dataTreeview.column('rank', width=2, anchor="center")
dataTreeview.column('link', width=20, anchor="center")
dataTreeview.column('title', width=350, anchor="center")
dataTreeview.column('rating', width=15, anchor="center")

dataTreeview.heading('rank', text='&#x6392;&#x540D;')
dataTreeview.heading('link', text='&#x94FE;&#x63A5;')
dataTreeview.heading('title', text='&#x540D;&#x79F0;')
dataTreeview.heading('rating', text='&#x70ED;&#x5EA6;')
dataTreeview.place(x=200, y=180, width=1000, height=300)

#&#x6EDA;&#x52A8;&#x6761;
s = tkinter.Scrollbar(dataTreeview, command=dataTreeview.yview)
s.pack(side="right", fill="y")
dataTreeview.config(yscrollcommand=s.set)
win.mainloop()

2.1思路

根据前面爬取的数据得到的数据库,实现GUI界面与SQLite数据库相连,即可查看排行榜信息,并且实现了能够查看全部信息,或者根据动漫名称关键字搜索可得相关信息。

除了可以按照标题查找,还能在此基础上拓展出按照热度、链接查找等功能。

2.2运行结果

Python3爬虫(sqlite3存储信息)--AGE动漫网站排行榜

2.3GUI设计难点

2.3.1按标题查询–模糊查询

content=”‘%”+selecttitle.get()+”%'”

Original: https://blog.csdn.net/To_Ma_To/article/details/119138812
Author: 王者热爱去地下城复活
Title: Python3爬虫(sqlite3存储信息)–AGE动漫网站排行榜

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816673/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球