【发布时间】:2020-12-29 16:14:12
【问题描述】:
我使用以下代码从网站上抓取图片网址。我接下来尝试实现的是将以“.jpg”结尾的 url 保存到本地文件夹中,该文件夹可以是 py 代码的位置。 我设法抓取和访问网址并在该位置创建一个文件夹,但我不知道如何保存它们。 这是我的代码,任何想法都非常感谢
from selenium import webdriver
import requests
import os
from bs4 import BeautifulSoup
import urllib
import urllib.request
from urllib.request import urlretrieve
import sys
if sys.version_info[0] >= 3:
from urllib.request import urlretrieve
else:
# if Not Python 3
from urllib import urlretrieve
site = 'https://www.amazon.de/dp/B077S8N26F'
directory = os.path.dirname(os.path.realpath(__file__)) + '/image_folder/'
if not os.path.exists(directory):
os.makedirs(directory)
driver = webdriver.Chrome()
driver.get(site)
soup = BeautifulSoup(driver.page_source, 'html.parser')
img_tags = soup.find_all('img')
urls = [img['src'] for img in img_tags]
for url in urls:
print(url)
#only the links that end with .jpg
images = [im for im in urls if im.endswith(".jpg")]
print(images)
for im in images:
#here is the missing part that saves urls into the folder created
【问题讨论】:
标签: python beautifulsoup save