I've probably spent too long on this already but I'm finding it hard to understand why I'm getting a FileNotFoundError: [Errno 2] No such file or directory: when the only difference I can see is the link. Using Beautiful Soup
Objective: Download an image and place in a different folder which works fine except on some .jpg files. I've tried different types of paths and striping the file names but its the same problem.
Test images:
http://img2.rtve.es/v/5437650?w=1600&preview=1573157283042.jpg # Not Working
http://img2.rtve.es/v/5437764?w=1600&preview=1573172584190.jpg #Works perfect
Here is the function:
def get_thumbnail():
'''
Download image and place in the images folder
'''
soup = BeautifulSoup(r.text, 'html.parser')
# Get thumbnail image
for preview in soup.findAll(itemprop="image"):
preview_thumb = preview['src'].split('//')[1]
# Download image
url = 'http://' + str(preview_thumb).strip()
path_root = Path(__file__).resolve().parents[1]
img_dir = str(path_root) + '\\static\\images\\'
urllib.request.urlretrieve(url, img_dir + show_id() + '_' + get_title().strip()+ '.jpg')
Other functions used:
def show_id():
for image_id in soup.findAll(itemprop="image"):
preview_id = image_id['src'].split('/v/')[1]
preview_id = preview_id.split('?')[0]
return preview_id
def get_title():
title = soup.find('title').get_text()
return title
All I can work out is the problem must be finding the images folder for the first image but the second works perfect.
This is the error I keep getting and it seems to be breaking at request.py
Thanks for any input.