html - 如何从 python 检索的 url 将照片下载到桌面文件夹?
问题描述
import urllib
import urllib.request
from bs4 import BeautifulSoup
theurl = "https://www.opi.com/shop-products/nail-polish-powders/nail-lacquer"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")
for img in soup.select('#all_nail_lacquer [typeof="foaf:Image"][data-src]'):
print(img['data-src'].replace('shelf_image', 'photos')) # <-- this is URL to hi-res image
print('-' * 80)
我得到了这个程序来收集与产品照片相关的所有链接。现在,我希望能够自动打开每个链接并将照片保存到特定文件夹。我该怎么做呢?
解决方案
要下载图像,您可以使用 urllib 模块。
import urllib
import urllib.request
from bs4 import BeautifulSoup
folder = "C:/tmp/"
theurl = "https://www.opi.com/shop-products/nail-polish-powders/nail-lacquer"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")
for img in soup.select('#all_nail_lacquer [typeof="foaf:Image"][data-src]'):
picurl = img['data-src'].replace('shelf_image', 'photos')
print(picurl) # <-- this is URL to hi-res image
print('-' * 80)
f1 = picurl.split('/')[-1] # living-on-the-bula-vard-nlf81-nail-lacquer-22006698181_12_0_0_0.jpg?itok=6RDDxV8f
f2 = f1.split('?')[0] # living-on-the-bula-vard-nlf81-nail-lacquer-22006698181_12_0_0_0.jpg
urllib.request.urlretrieve(picurl, folder + f2)