我有一個使用 Python 從 Tumblr 抓取影像的專案。我想下載在我抓取的鏈接上找到的影像。
這是整個代碼:
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')
在這篇文章的回答之后,我更改了代碼并使用了request和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')
現在我收到了這個錯誤:
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'
uj5u.com熱心網友回復:
您必須request再次使用影像 URL。然后您可以獲得原始形式的回應并保存影像
用下面的代碼替換代碼 -
for source, image_urls in data.items():
for url in image_urls:
# make request with image url
img_scrape = requests.get(url, stream=True)
if img_scrape.status_code == 200:
with open(source,'wb') as f:
img_scrape.raw.decode_content = True
# save the image raw format
shutil.copyfileobj(r.raw, f)
print('Image sucessfully Downloaded: ',source)
else:
print('Image Couldn\'t be retrieved')
輸出 -
Image sucessfully Downloaded: pics-bae
Image sucessfully Downloaded: pics-bae
Image sucessfully Downloaded: laravel
Image sucessfully Downloaded: huariqueje
Image sucessfully Downloaded: sweetd3lights
Image sucessfully Downloaded: shesinthegrove
Image sucessfully Downloaded: careful-disorder
Image sucessfully Downloaded: beifongkendo
Image sucessfully Downloaded: traveltoslovenia
Image sucessfully Downloaded: traveltoslovenia
Image sucessfully Downloaded: traveltoslovenia
Image sucessfully Downloaded: bradsbackpack
Image sucessfully Downloaded: pensamentsisomnis
Image sucessfully Downloaded: frankfurtphoto
........
..........
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483251.html
