从桌面运行Python脚本
我已经在我的电脑上安装了Anaconda,我还将Anaconda添加到path。我制作了一个脚本,它在Jupyter中运行良好,当我从Anaconda提示符终端调用它时,它也运行良好。然而,当我试图从桌面以Python脚本的形式执行它时,什么也没有发生。我甚至没有得到输出,所以我可以看到哪里出了问题。以下是脚本:从桌面运行Python脚本,python,tkinter,Python,Tkinter,我已经在我的电脑上安装了Anaconda,我还将Anaconda添加到path。我制作了一个脚本,它在Jupyter中运行良好,当我从Anaconda提示符终端调用它时,它也运行良好。然而,当我试图从桌面以Python脚本的形式执行它时,什么也没有发生。我甚至没有得到输出,所以我可以看到哪里出了问题。以下是脚本: from bs4 import BeautifulSoup import requests import re import urllib.request import os from
from bs4 import BeautifulSoup
import requests
import re
import urllib.request
import os
from tkinter import *
from PIL import Image, ImageTk
import tkinter as tk
from tkinter import Tk, Menu, Canvas
import datetime
from pathlib import Path
url = requests.get("https://politiken.dk/underholdning/wulffmorgenthaler/")
html = url.text
soup = BeautifulSoup(html,'lxml')
main = soup.find_all("a", attrs={"data-prefetchable":"true"})
list_of_links = []
for i in main:
list_of_links.append(re.findall(r'(https://politiken.dk/underholdning/wulffmorgenthaler/.{1,34}(?="))', str(i))[0])
list_of_links = list(dict.fromkeys(list_of_links))
counter = 0
def next_day():
global counter
counter += 1
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
def prev_day():
global counter
if counter == 0:
pass
else:
counter -= 1
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
def image_downloader(date_numbers):
global counter
my_file = Path(fr"C:\Users\Peter\Desktop\Wolfmorgenthaler/{date_numbers}.jpg")
if my_file.exists():
open_img(date_numbers)
elif counter<len(list_of_links):
new_url = requests.get(list_of_links[counter])
new_html = new_url.text
new_soup = BeautifulSoup(new_html,'html')
new_main = new_soup.find_all("img", attrs={"class":"media__image image--fullwidth"})
new_picture_links = re.findall(r'https.+? ', str(new_main[0]))
final_link = new_picture_links[0]
fullfilename = os.path.join(r"C:\Users\Peter\Desktop\Wolfmorgenthaler", f"{date_numbers}.jpg")
urllib.request.urlretrieve(final_link, fullfilename)
open_img(date_numbers)
else:
counter = counter - 1
def open_img(name):
filepath = fr"C:\Users\Peter\Desktop\Wolfmorgenthaler/{name}.jpg"
img = Image.open(filepath)
img = img.resize((960, 449), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
panel = Label(root, image=img)
panel.image = img
panel.grid(row = 1, columnspan=4)
root = Tk()
root.geometry("964x483")
root.resizable(width=True, height=True)
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
btn1 = Button(root, text='Næste billede', command=next_day, width = 67).grid(row = 2, column = 2)
btn2 = Button(root, text='Forrige billede', command=prev_day, width = 67).grid(row = 2, column = 1)
root.mainloop()
从bs4导入美化组
导入请求
进口稀土
导入urllib.request
导入操作系统
从tkinter进口*
从PIL导入图像,ImageTk
将tkinter作为tk导入
从tkinter导入Tk、菜单、画布
导入日期时间
从pathlib导入路径
url=请求。获取(“https://politiken.dk/underholdning/wulffmorgenthaler/")
html=url.text
soup=BeautifulSoup(html,'lxml')
main=soup.find_all(“a”,attrs={“数据预取”:“true”})
链接列表=[]
我主要是:
链接列表。追加(关于findall(r'(https://politiken.dk/underholdning/wulffmorgenthaler/.{1,34}(?=“)”,str(i))[0])
链接列表=列表(dict.fromkeys(链接列表))
计数器=0
定义下一天():
全局计数器
计数器+=1
今天=datetime.date.today()
date=today-datetime.timedelta(天=计数器)
最终日期=日期。标准时间(“%m%d”)
图像下载程序(最终日期)
定义上一天():
全局计数器
如果计数器==0:
通过
其他:
计数器-=1
今天=datetime.date.today()
date=today-datetime.timedelta(天=计数器)
最终日期=日期。标准时间(“%m%d”)
图像下载程序(最终日期)
def图像下载器(日期编号):
全局计数器
my_file=Path(fr“C:\Users\Peter\Desktop\Wolfmorgenthaler/{date_numbers}.jpg”)
如果my_file.exists():
打开img(日期和编号)
elif计数器这是这里发现的最奇怪的解决方案:
“我的解决方法:我复制了以下文件
libcrypto-1_1-x64.*
libssl-1_1-x64.*
from D:\Anaconda3\Library\bin to D:\Anaconda3\DLLs.
它是一种魅力!“终端窗口在您阅读之前是否会闪烁打开并立即关闭?是的!没错。知道原因吗?通常当终端窗口闪烁并立即关闭时,会出现一些错误,阻止它运行,或者脚本在您看到任何东西之前就完成了。请尝试在桌面上打开命令提示符并运行python f。”从那里输入文件,以便您可以看到错误(如果有)。或者,您可以使用批处理文件,然后在其末尾键入pause
,这样命令提示符就不会在没有输入的情况下关闭。好的,这真的很奇怪。当我通过Anaconda提示符运行脚本时,它工作得很好。但是,当我通过windows命令提示符运行脚本时,会出现以下错误:requests.exceptions.SSLError:HTTPSConnectionPool(host='politiken.dk',port=443):url超过最大重试次数:/underholding/wulffmorgenthaler/(由SSLError引起(“无法连接到HTTPS url,因为SSL模块不可用”))这真的很奇怪。。。