• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

通过Python爬虫获取小说网站GUI数据,保姆级教学

武飞扬头像
红目香薰
帮助1

学新通

通过Python爬虫获取【小说网站GUI】数据,保姆级教学


目录

通过Python爬虫获取【小说网站GUI】数据,保姆级教学

前言

示例环境

爬取目标:

爬取代码

核心技术点:

注意点:

源码:

爬取结果:


前言

        所有的前置环境以及需要学习的基础我都放置在【Python基础(适合初学-完整教程-学习时间一周左右-节约您的时间)】中,学完基础咱们再配置一下Python爬虫的基础环境【看完这个,还不会【Python爬虫环境】,请你吃瓜】,搞定了基础和环境,我们就可以相对的随心所欲的获取想要的数据了,所有的代码都是我一点点写的,都细心的测试过,如果某个博客爬取的内容失效,私聊我即可,留言太多了,很难看得到,本系列的文章意在于帮助大家节约工作时间,希望能给大家带来一定的价值。

示例环境

系统环境:win11

开发工具:PyCharm Community Edition 2022.3.1

Python版本:Python 3.9.6

资源地址:链接:https://pan.百度.com/s/1UZA8AAbygpP7Dv0dYFTFFA 提取码:7m3e

MySQL:5.7,url=【rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com】,user=【qwe8403000】,pwd=【Qwe8403000】,库比较多,自己建好自己的,别跟别人冲突。

学新通

爬取目标:

输入对应的id就行直接获取其内容,保存在项目执行的位置。

学新通

爬取代码

核心技术点:

1、requests返回的数据格式需要看网页的具体编码

2、parsel根据接口返回数据结构来选择对应的解析方案

3、Progressbar进度条控制

注意点:

1、多线程执行的时候只填写函数名称即可,不需要写括号

2、不需要使用bar.start(),直接进行bar["value"]值修改后进行root.update()即可刷新页面。

源码:

  1.  
    import requests
  2.  
    import parsel
  3.  
    import uuid
  4.  
    import time
  5.  
    import random
  6.  
    import os
  7.  
    from tkinter import *
  8.  
    import threading
  9.  
    import tkinter.messagebox as messagebox
  10.  
    import tkinter as tk
  11.  
    from tkinter import ttk
  12.  
     
  13.  
    root = Tk()
  14.  
     
  15.  
    # 进度条count
  16.  
    barCount = []
  17.  
     
  18.  
    screenwidth = root.winfo_screenwidth()
  19.  
    screenheight = root.winfo_screenheight()
  20.  
    dialog_width = 800
  21.  
    dialog_height = 220
  22.  
    # 前两个参数是窗口的大小,后面两个参数是窗口的位置
  23.  
    root.geometry(
  24.  
    "%dx%d %d %d" % (dialog_width, dialog_height, (screenwidth - dialog_width) / 2, (screenheight - dialog_height) / 2))
  25.  
     
  26.  
    root.title("(红目香薰提供)主页地址:【https://laoshifu.blog.csdn.net/】")
  27.  
    Label(root, text='例如:https://book.zongheng.com/book/1228049.html中的【1228049】一组数字').grid(row=0, column=0)
  28.  
     
  29.  
    Label(root, text='url:输入文章id即可').grid(row=1, column=0)
  30.  
    e = Entry(root, width=100)
  31.  
    e.grid(row=3, column=0, padx=15, pady=5)
  32.  
     
  33.  
    root.resizable(height=False, width=False)
  34.  
     
  35.  
    headers = {
  36.  
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
  37.  
    }
  38.  
     
  39.  
    listChild = []
  40.  
    listDate = []
  41.  
    mTitle = []
  42.  
    # 文章链接与标题独立列表
  43.  
    a_href_list = ["", ""]
  44.  
    # 存放文章链接与标题数组列表
  45.  
    a_href_arr = []
  46.  
    # 存放文章全文
  47.  
    infoDate = []
  48.  
     
  49.  
     
  50.  
    def GetUrl(url):
  51.  
    try:
  52.  
    html = requests.get(url, headers=headers)
  53.  
    sel = parsel.Selector(html.text)
  54.  
    # 获取主Title
  55.  
    mTitle.append(sel.css(".book-meta h1::text").getall()[0])
  56.  
    href = sel.css(".volume-list ul a::attr(href)").getall()
  57.  
    # 获取标题
  58.  
    text = sel.css(".volume-list ul a::text").getall()
  59.  
    for item1, item2 in zip(href, text):
  60.  
    a_href_list = ["", ""]
  61.  
    a_href_list[0] = item1
  62.  
    a_href_list[1] = item2
  63.  
    a_href_arr.append(a_href_list)
  64.  
    print("列表获取完毕")
  65.  
    except:
  66.  
    print("解析异常")
  67.  
     
  68.  
     
  69.  
    def GetTxt(url, title):
  70.  
    html = requests.get(url, headers=headers)
  71.  
    sel = parsel.Selector(html.text)
  72.  
    # 文章
  73.  
    info = sel.css(".content p::text").getall()
  74.  
    infoDate.append(title "\r\n")
  75.  
    for item in info:
  76.  
    infoDate.append(item "\r\n")
  77.  
     
  78.  
     
  79.  
    def show():
  80.  
    """
  81.  
    开启线程1,2
  82.  
    :return:
  83.  
    """
  84.  
    t1 = threading.Thread(target=showFun, name="T1")
  85.  
    t2 = threading.Thread(target=startThread, name="T2")
  86.  
    t1.start()
  87.  
    t2.start()
  88.  
     
  89.  
     
  90.  
    def showFun():
  91.  
    try:
  92.  
    # 获取的文本
  93.  
    oldUrl = e.get()
  94.  
    # 1159606
  95.  
    bookIdDir = "https://book.zongheng.com/showchapter/{0}.html".format(oldUrl)
  96.  
    GetUrl(bookIdDir)
  97.  
     
  98.  
    for item in a_href_arr:
  99.  
    barCount.append("1")
  100.  
    GetTxt(item[0], item[1])
  101.  
    print(item[1], "获取文章完毕")
  102.  
    time.sleep(random.uniform(0.5, 1.5))
  103.  
     
  104.  
    with open(str.format("{0}.txt", mTitle[0]), "w ", encoding="utf-8") as f:
  105.  
    f.write("".join(infoDate))
  106.  
    f.close()
  107.  
    os.system("explorer .")
  108.  
    messagebox.showinfo("提示", str.format("{0}.txt", mTitle[0]), "保存完毕")
  109.  
    except:
  110.  
    print("列表获取结束")
  111.  
     
  112.  
     
  113.  
    def find():
  114.  
    """打开默认页面"""
  115.  
    os.system(
  116.  
    '"C:/Program Files/Internet Explorer/iexplore.exe" https://book.zongheng.com/store/c0/c0/b0/u21/p1/v0/s1/t0/u0/i1/ALL.html')
  117.  
     
  118.  
     
  119.  
    def startThread():
  120.  
    """进度条读条"""
  121.  
    time.sleep(2)
  122.  
    bar["value"] = 0
  123.  
    while True:
  124.  
    if len(a_href_arr) > 1:
  125.  
    go = (float(len(barCount)) / len(a_href_arr)) * 100
  126.  
    bar["value"] = go
  127.  
    bar.Progress = "{0}%".format(go)
  128.  
    root.update()
  129.  
    time.sleep(5)
  130.  
    if go > 99:
  131.  
    break
  132.  
     
  133.  
     
  134.  
    Button(root, text='小说下载', width=10, command=show).grid(row=4, column=0, padx=10, pady=5)
  135.  
    Button(root, text='查找小说', width=10, command=find).grid(row=5, column=0, padx=10, pady=5)
  136.  
    Label(root, text='程序进度条').grid(row=6, column=0)
  137.  
     
  138.  
    bar = ttk.Progressbar(root, length=600, cursor='spider', mode="determinate", maximum=100, orient=tk.HORIZONTAL)
  139.  
    bar.grid(row=7, column=0)
  140.  
    mainloop()
学新通

爬取结果:

都放在一个txt文档中了。

学新通

后续我会打包生成下载GUI工具直接下载即可使用。

下载地址:【方便下载小说,小说都是公开免费的,放心下载

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhabakg
系列文章
更多 icon
同类精品
更多 icon
继续加载