'File Not found Erro making a webscrapper with python
I am following a tutorial (The Complete Python Course: Beginner to Advanced!) and am stuck on the Python image scrapper. I am trying to download some pictures from the internet, but the code keeps giving me errors. Here's what I have at the moment:
from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
import random
search = input("Search for:")
params = {"q": search}
r = requests.get("http://bing.com/images/search", params=params)
soup = BeautifulSoup(r.text, "html.parser")
links = soup.findAll("img", {"class": "mimg"})
for item in links:
img_obj = requests.get(item.attrs["src"])
print("getting", item.attrs["src"])
title = "img" + str(random.randint(1, 999)) # item.attrs["src"].split("/")[-1]
img = Image.open(BytesIO(img_obj.content))
img.save("C:\\Users\\user\\PycharmProjects\\webscrapery\\scraped_images\\" + title, img.format)
and the error is:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\webscrapery\images.py", line 19, in <module>
img.save(r"C:\\Users\\user\\PycharmProjects\\webscrapery\\scraped_images\\" + title, img.format)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 2209, in save
fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\\\Users\\\\user\\\\PycharmProjects\\\\webscrapery\\\\scraped_images\\\\img84'
From what I understand, the problem comes from the script trying to open the image before the image is saved at all. However I need to open the image that exists in the link that is stored in the img_obj variable. But because it's not saved in the directory, it cannot continue?
here is my previous question regarding this, along with the help I got there.
Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
