0

I have installed Anaconda on my PC, and I've also added Anaconda to path. I've made a script which works fine in Jupyter, and it also works fine when I call it from the Anaconda Prompt terminal. However, when I try to execute it as a Python script from my desktop, nothing happens. I don't even get an output, so I can see what's going wrong. This is the script:

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()

I just reset my PC today, and it worked just fine before I reset it. I have no idea how to get it to work. I would like to just have it be a clickable icon on my desktop, which executes the script.

Executing it with a batch file did not work either. Any ideas?

peterlravn
  • 43
  • 8
  • Does a terminal window flash open and immediately close before you can read it? – SyntaxVoid Jun 02 '21 at 20:45
  • YES! Exactly. Any idea why this is? – peterlravn Jun 02 '21 at 20:45
  • Usually when the terminal window flashes and immediately closes, there is some error preventing it from running OR your script finishes before you see anything. Try opening a command prompt in your Desktop and running the python file from there so you can see the error (if there is one). – randomcoder Jun 02 '21 at 20:51
  • Alternatively, you can use your batch file and then type in `pause` at the end of it so the command prompt doesn't close without input. – randomcoder Jun 02 '21 at 20:52
  • Okay, so this is really weird. When I run the script through Anaconda Prompt, it works perfectly. However, when I run it through the windows Command Prompt, I get the following error: requests.exceptions.SSLError: HTTPSConnectionPool(host='politiken.dk', port=443): Max retries exceeded with url: /underholdning/wulffmorgenthaler/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) This is really weird... – peterlravn Jun 02 '21 at 20:54
  • I figured it out by googling that error string. Thanks for leading me on the correct path! – peterlravn Jun 02 '21 at 20:59

1 Answers1

0

This is the weirdest solution found here:

Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website

" My workaround: I have copied the following files

libcrypto-1_1-x64.*
libssl-1_1-x64.*
from D:\Anaconda3\Library\bin to D:\Anaconda3\DLLs.

And it works as a charm! "

peterlravn
  • 43
  • 8