首页 > 编程语言 >如何用python做一个exe程序快速爬取文章?

如何用python做一个exe程序快速爬取文章?

时间:2023-07-26 11:35:22浏览次数:59  
标签:exe python text app 爬取 window tk input image

我用了99藏书网作为例子

九九藏书网 (99csw.com)

注:本程序主要用于快速复制99藏书网中的小说,有些参数我要在开头先解释清楚

 

一、导入库

import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

 二、做一个背景函数

这个背景函数主要是用于GUI可视化背景的设置

def set_background_image(window, image_path):
    # 创建一个PhotoImage对象
    background_image = tk.PhotoImage(file=image_path)

    # 将PhotoImage对象设置为窗口的背景
    background_label = tk.Label(window, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    # 保存PhotoImage对象到窗口属性中,避免被垃圾回收
    window.background = background_image

 

三、做GUI主页面

app = tk.Tk()

app.title("九九藏书网")
app.geometry('1920x1080')
set_background_image(app,"哆唻A梦.png")

# 添加一个Text组件用于显示爬取的内容
text = tk.Text(app, width=50, height=40, font=('Arial', 12), wrap=tk.WORD)
text.place(x=10, y=10)

lb = tk.Label(app, text='欢迎来到“九九藏书网”书籍采集获取系统', width=40, height=1, fg='black', font=('Arial', 18))
lb.pack()

bt = tk.Button(app, text="开始", width=10, command=main, activebackground='red')
bt.pack()

app.mainloop()

 

四、做GUI次级界面

def main():
    input_window = tk.Toplevel(app)
    input_window.geometry(app.geometry())
    set_background_image(input_window, "哆唻A梦.png")
    lb1 = tk.Label(input_window, text='请输入书本号', width=40, height=10, fg='black', font=8)
    lb1.place(x=1, y=1)
    entry1 = tk.Entry(input_window)
    entry1.place(x=130, y=220)
    lb2 = tk.Label(input_window, text='请输入前言的章节号', width=40, height=10, fg='black', font=8)
    lb2.place(x=1, y=250)
    entry2 = tk.Entry(input_window)
    entry2.place(x=130, y=470)
    lb3 = tk.Label(input_window, text='请输入最后一个章节的章节号', width=40, height=10, fg='black', font=8)
    lb3.place(x=1, y=500)
    entry3 = tk.Entry(input_window)
    entry3.place(x=130, y=720)
    confirm_button = tk.Button(input_window, text="确认", command=lambda: process_input1(entry1.get(), entry2.get(), entry3.get()))
    confirm_button.place(x=180, y=750)

 

五、写一个爬虫函数

先找到存储正文的模块

 

def process_input1(a, b, c):
    passage = 0
    start_chapter = int(b)
    end_chapter = int(c)

    for i in range(start_chapter, end_chapter + 1):
        driver = webdriver.Chrome()
        
        url = f'https://www.99csw.com/book/{a}/{i}.htm'
        driver.get(url)

        content_element = driver.find_element(By.XPATH, '//div[@id="content"]')
        content = content_element.text
        print(content)

        text.insert(tk.END, content + '\n')

        filename = f"./活着第{i}章.txt"
        if passage == 0:
            with open('./活着前言.txt', "w", encoding='utf-8') as f:
                f.write(content)
            passage += 1
            continue
        else:
            with open(filename, "w", encoding="utf-8") as f:
                f.write(content)
            continue

 

六、完整代码

import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

# 一个背景函数
def set_background_image(window, image_path):
    # 创建一个PhotoImage对象
    background_image = tk.PhotoImage(file=image_path)

    # 将PhotoImage对象设置为窗口的背景
    background_label = tk.Label(window, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    # 保存PhotoImage对象到窗口属性中,避免被垃圾回收
    window.background = background_image


def main():
    input_window = tk.Toplevel(app)
    input_window.geometry(app.geometry())
    set_background_image(input_window, "哆唻A梦.png")
    lb1 = tk.Label(input_window, text='请输入书本号', width=40, height=10, fg='black', font=8)
    lb1.place(x=1, y=1)
    entry1 = tk.Entry(input_window)
    entry1.place(x=130, y=220)
    lb2 = tk.Label(input_window, text='请输入前言的章节号', width=40, height=10, fg='black', font=8)
    lb2.place(x=1, y=250)
    entry2 = tk.Entry(input_window)
    entry2.place(x=130, y=470)
    lb3 = tk.Label(input_window, text='请输入最后一个章节的章节号', width=40, height=10, fg='black', font=8)
    lb3.place(x=1, y=500)
    entry3 = tk.Entry(input_window)
    entry3.place(x=130, y=720)
    confirm_button = tk.Button(input_window, text="确认", command=lambda: process_input1(entry1.get(), entry2.get(), entry3.get()))
    confirm_button.place(x=180, y=750)

def process_input1(a, b, c):
    passage = 0
    start_chapter = int(b)
    end_chapter = int(c)

    for i in range(start_chapter, end_chapter + 1):
        driver = webdriver.Chrome()
        
        url = f'https://www.99csw.com/book/{a}/{i}.htm'
        driver.get(url)

        content_element = driver.find_element(By.XPATH, '//div[@id="content"]')
        content = content_element.text
        print(content)

        text.insert(tk.END, content + '\n')

        filename = f"./活着第{i}章.txt"
        if passage == 0:
            with open('./活着前言.txt', "w", encoding='utf-8') as f:
                f.write(content)
            passage += 1
            continue
        else:
            with open(filename, "w", encoding="utf-8") as f:
                f.write(content)
            continue


app = tk.Tk()

app.title("九九藏书网")
app.geometry('1920x1080')
set_background_image(app,"哆唻A梦.png")

# 添加一个Text组件用于显示爬取的内容
text = tk.Text(app, width=50, height=40, font=('Arial', 12), wrap=tk.WORD)
text.place(x=10, y=10)

lb = tk.Label(app, text='欢迎来到“九九藏书网”书籍采集获取系统', width=40, height=1, fg='black', font=('Arial', 18))
lb.pack()

bt = tk.Button(app, text="开始", width=10, command=main, activebackground='red')
bt.pack()

app.mainloop()

 

七、打包python程序

1、下载打包python程序用的pyinstaller

在终端输入

pip install pyinstaller

 

2、打包python程序

pyinstall -F python文件名

 这时候就生成了一个exe文件

3、找到exe文件位置,点击即可运行程序(如果运行不成功,检查exe文件包里面是否含有python程序中用到的一些图片字体等文件,若没有,自行加入)

现在给大家看一下运行结果

 爬取的内容存储在了左侧text框中

 

标签:exe,python,text,app,爬取,window,tk,input,image
From: https://www.cnblogs.com/dashingbaibaoxiang/p/17581871.html

相关文章

  • python实现自动切换壁纸(win10)
    因为本人工作环境特殊,很多软件的下载很麻烦,而且违规。然后发现域策略有变更,之前貌似不可以自己换壁纸。我是一个对任何重复的事物都十分容易腻的人,壁纸也包括在内,所以决定写一个自动切换壁纸的脚本importosimportctypesimporttimefromdatetimeimportdatetime#放......
  • Linux版python安装教程
    如果你希望在CentOS上使用源码编译的方式安装Python3,请按照以下步骤进行操作:安装编译工具和依赖项:在开始编译前,需要安装一些编译工具和Python3的依赖项。在终端中运行以下命令:sudoyumgroupinstall"DevelopmentTools"sudoyuminstallopenssl-develbzip2-devel......
  • python学习01:Python基础语法与数据类型
    一、Python注释通常用于解释代码,这段打开主要是想表达什么意思,注释后的代码不会再代码中运行,例如:#打印HelloWorldprint("HelloWorld")注释的方式:#python注释(快捷键:Ctrl+/(选中你想注释的代码就可全部注释掉))=========>单行注释''''print('hello') ''''''�......
  • python logurur日志用法记录
    importsysfromloguruimportloggerlogger.configure(handlers=[{"sink":sys.stderr,"format":"{time:YYYY-MM-DDHH:mm:ss.SSS}|<lvl>{level:8}</>|{name}:{module}:{line:4}|<cyan>mymodu......
  • python装饰器
    目录函数可以作为参数函数可以作为返回值装饰器方法装饰器类装饰器函数可以作为参数函数本身在python里面是一个对象,也可以作为参数被传入另一个参数里.defdouble(x): returnx*2deftriple(x): returnx*3defcalc_number(func,x): print(func(x)) calc_number(......
  • JDBC preparedStatement.executeQuery() 与 preparedStatement.executeQuery(sql)
    preparedStatement.executeQuery()这个方法是执行带占位符、已经预编译的sql命令而它--->preparedStatement.executeQuery(sql)这个方法是执行未预编译、完整的sql命令,而不是预编译的sql命令preparedStatement不是应该执行预编译的sql吗?是这样的,但是preparedStatement还兼......
  • 盘点一个通过python大批量插入数据到数据库的方法
    大家好,我是皮皮。一、前言前几天在Python白银群【鶏啊鶏】问了一个Python数据存入数据库的问题,一起来看看吧。各位大佬我想请教下通过python大批量插入数据到数据库的方法目前我在用的操作是以下这个模式:sql=''foriinlist:sql="insertXXX表(地址,单号,缸号,状态,备......
  • python 函数 定义参数类型
    Python函数参数类型的定义在Python中,函数是一种非常重要的编程概念,它可以将一段代码逻辑封装成一个可重复使用的块。函数的参数类型的定义对于函数的正确使用和调用至关重要。本文将介绍Python中函数参数类型的定义方法,并给出一些代码示例。为什么要定义函数参数类型Python......
  • python 固定长度数组
    python固定长度数组在Python中,数组是一种常见的数据结构,用于存储相同类型的元素。通常,我们可以使用列表(List)来表示数组。然而,Python中的列表是可变长度的,这意味着我们可以随时向列表中添加或删除元素。但在某些情况下,我们需要固定长度的数组,即不能增加或删除元素。本文将介绍如何......
  • python 根据文字生成图片
    使用Python生成图片导言在这篇文章中,我将向您展示如何使用Python根据文字生成图片。这个过程可以通过以下几个步骤实现:创建一个空白图片;添加文字到图片中;设置图片的样式,如字体、颜色和大小;保存生成的图片。现在,让我们一步一步地来实现这个过程。步骤下面的表格展示了整......