我正在写一段代码,在按下一个按钮后创建一个文本文件.在代码的最后,在创建文本文件后,我试图重新打开文件,并将所有的行粘贴在一个输入框或多行标签上,任何帮助是好的。
先谢谢你
我知道我的代码很烂,因为我真的不懂python.但这是我的代码。
import tkinter as tk
from tkinter import messagebox
from bs4 import BeautifulSoup
import requests
import os
HEIGHT = 500
WIDTH = 600
def get_anime_function (entry):
if entry == "":
messagebox.showerror("Error", "No hay link que buscar")
else:
url = entry
txt = url.replace('https://animeflv.net/anime/', '')
txt = txt + ".txt"
r = requests.get(url)
for line in r.text.splitlines():
if 'var episodes =' in line:
line = line.replace(' ', '')
line = line.replace('var episodes = ', '')
line = line.replace('[', '')
line = line.replace(']', '')
line = line.replace(';', '')
break
first_char = line[0:3]
first_char = first_char.split(",",maxsplit=1)[0]
first_char = int(first_char)
url = url.replace('/anime/', '/ver/')
for x in range(1, first_char+1):
url2 = url + "-" + str(x)
r2 = requests.get(url2)
html_content = r2.text
soup = BeautifulSoup(html_content, 'lxml')
links = soup.find_all('a', href=True, attrs={'class':'Button Sm fa-download'})
text = str(links)
text = text.replace('[<a class="Button Sm fa-download" href="', '')
text = text.replace('" rel="nofollow" target="_blank">DESCARGAR</a>]', '')
text = text.replace('" rel="nofollow" target="_blank">DESCARGAR</a>, <a class="Button Sm fa-download" href="', '')
text = text.replace('http://ouou.io/s/y0d65LCP?s=', '') #is ouo but the forum dont allow it
text = text.replace('http', '\nhttp')
text = os.linesep.join([s for s in text.splitlines() if s])
text_file = open(txt, "a")
for item in text.split():
if "zippyshare" in item:
n = text_file.write(item + '\n')
text_file.close()
f = open(txt, "r")
lower_entry['text'] = f
f.close()
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
#background_image = tk.PhotoImage(file='background.png')
#background_label = tk.Label(root, image=background_image)
#background_label.place(relwidth=1, relheight=1)
frame = tk.Frame(root)
frame.place(relx = 0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
label = tk.Label(frame, text="Link de AnimeFLV", anchor='w')
label.place(relx = 0, rely=0, relwidth=0.65, relheight=0.4)
entry = tk.Entry(frame, font=40, justify='left')
entry.place(rely = 0.4, relwidth=0.65, relheight=0.5)
button = tk.Button(frame, text="Ejecutar", font=60, command=lambda: get_anime_function(entry.get()))
button.place(relx = 0.7, relwidth=0.3, relheight=1)
lower_frame = tk.Frame(root)
lower_frame.place(relx = 0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
lower_entry = tk.Entry(lower_frame, font=60)
lower_entry.place(relwidth=1, relheight=1)
root.mainloop()
欢迎来到编程,并继续前进! 你会变得更好,不要对自己太苛刻! 下面是一个读取文本文件并在标签上显示内容的例子。
from tkinter import *
root = Tk()
with open("SomeTextFile.txt", "r") as f:
Label(root, text=f.read()).pack()
root.mainloop()