Jun-05-2024, 06:51 PM
from urllib.request import urlopen from pathlib import Path with open(r'D:\Desktop\sites.txt', 'r', encoding='UTF-8') as file: while line := file.readline(): myurl = line.rstrip() myfold = myurl[8:10] myfn = myurl[8:12] myfilen = myfn + 'txt' Path("D:/Desktop/" + myfold).mkdir(parents=True, exist_ok=True) with urlopen( myurl ) as webpage: content = webpage.read().decode() with open("D:/Desktop/" + myfold + "/" + myfilen , "w" ) as output: output.write( content )I have a list of urls named sites.txt
Each URL has the same length.
I am trying to loop through each site and
1. Take part of the url name and create a folder on the Desktop if it doesn't exist.
2. Take part of the url and create a filename.
3. Save the page source of each url as a text file within the corresponding folder
This seems to run in Python 3 without any errors, but if it's creating the txt files, they are somewhere else. It is not even creating the folders.
Sorry I am new to Python.
Thank you.