I had this python script given for personal purposes of collecting images from a website that requires the ID of the image within the URL, and each image has an ID in order and is same as other IDs with the difference of a small number (for example, one image could be ID 400.png, the other could be 420.png, the difference being 20 between those 2 IDs). Normally, you can just enter the URL on a browser and put the image’s ID and it’s extension (.png/.jpg) and view the image on the browser, but I would then need to manually add +1 or -1 to ID’s number to look for every image I need, so this script helps me do that automatically.
import requests
from PIL import Image
# 1719479856 is a valid sample for the variable {x}, you can check the link on a browser by replacing {x} with the ID
def writeToFile(numb, content, ext):
img = open(f"img_{numb}.{ext}", "wb")
img.write(content)
img.close()
img_final = Image.open(f"img_{numb}.{ext}")
img_final.show()
try:
x = int(input("starting val: "))
while True:
r = requests.get(url=f"https://ad-cdn.hrgame.com.cn/pvz2/upload/2024062717/{x}.jpg")
if r.status_code == 200:
print(f"found | {x}.jpg | code: {r.status_code}")
content = r.content
writeToFile(x, content, "jpg")
else:
print(f"jpg not found | {x} | code: {r.status_code}")
x += 1
except Exception as e:
print(f"error: {e}")
However today when I ran the script, it suddenly stopped working, and I have no idea what could have possibly caused that. The terminal will say “jpg not found” despite the .jpg existing for that ID of image, even more so is accessible and viewable on browser, so it couldn’t be the website/server/host blocking access.
Now at first I thought this would be the fault of Visual Studio Code, but later on I realized that this probably isn’t the case at all, as I tried running the script on cmd itself. Secondly, I tried different variations of the script, such as one that checks for both png and jpg at the same time for the same ID, or one that checks for pngs instead, but obviously they weren’t going to work and indeed did not work. My only idea is that this has to do with the website/server, and I have absolutely no idea what could it be. Any ideas? At the very least some confirmation that it could be the website’s/server’s fault?
Thank you for your answers, let me know if I’m vague at parts or if I’m making non-sense at parts.