【发布时间】:2019-09-03 21:15:28
【问题描述】:
我正在尝试从网站下载所有图像,但无法下载。如何从网站的特定部分下载所有图像并将其保存到我的目录?
下面的代码导出所有图像并将图像链接保存到 csv 文件,但我也希望图像也保存在我的目录中。
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup
my_url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphics%20card'
req = Request(my_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
page_soup = soup(webpage, "html.parser")
filename = "abc.csv"
f = open(filename, "w")
headers = "imagelink\n"
f.write(headers)
snackcrisps = page_soup.findAll("div",{"class":"divCategories divShops-newegg"})
crispitem = snackcrisps[0]
img = crispitem.findAll("div",{"class":"product_image_div productSmall_image_div_lit"})
img1 = img[0]
for img1 in img:
img2 = img1.findAll('img')
imageLink = img2[0].get('src')
print("imageLink: " + imageLink)
f.write(imageLink + "\n")
f.close()
如何将图像保存在本地目录中?需要帮助!!
非常感谢
【问题讨论】:
-
我是新手,不知道如何解决。有人可以为我解决这个问题吗?谢谢