I have a project which scrapes images from Tumblr using Python. I want to download the images found on the links I get scraping.
This is the entire code:
import requests
from bs4 import BeautifulSoup
import shutil
search_term = "landscape/recent"
posts_scrape = requests.get(f"https://www.tumblr.com/search/{search_term}")
soup = BeautifulSoup(posts_scrape.text, "html.parser")
articles = soup.find_all("article", class_="FtjPK")
data = {}
for article in articles:
try:
source = article.find("div", class_="vGkyT").text
for imgvar in article.find_all("img", alt="Image"):
data.setdefault(source, []).extend(
[
i.replace("500w", "").strip()
for i in imgvar["srcset"].split(",")
if "500w" in i
]
)
except AttributeError:
continue
for source, image_urls in data.items():
for url in image_urls:
if posts_scrape.status_code == 200:
url.raw.decode_content = True
with open(source,'wb') as f:
shutil.copyfileobj(url.raw, f)
print('Image sucessfully Downloaded: ',source)
else:
print('Image Couldn\'t be retrieved')
Following an answer on this post i changed the code and used request and shutil:
for source, image_urls in data.items():
for url in image_urls:
if posts_scrape.status_code == 200:
url.raw.decode_content = True
with open(source,'wb') as f:
shutil.copyfileobj(url.raw, f)
print('Image sucessfully Downloaded: ',source)
else:
print('Image Couldn\'t be retrieved')
And now I am getting this error:
Traceback (most recent call last):
File "/home/user/folder/Information.py", line 28, in <module>
url.raw.decode_content = True
AttributeError: 'str' object has no attribute 'raw'