如何将图片从网站保存到本地文件夹

2024-10-04 11:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将此网站的图片保存到文件夹中:

http://www.photobirdireland.com/garden-birds.html

我尝试过使用导入操作系统

from lxml import html
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs


class ImageScraper:
    def __init__(self, url, download_path):
        self.url = url
        self.download_path = download_path

        self.session = requests.Session()

    def scrape_images(self):

        html = urlopen(url)
        bs4 = bs(html, 'html.parser')
        images = bs4.find_all('img', {})


scraper = ImageScraper(url="http://www.photobirdireland.com/garden-birds.html")
scraper.scrape_images() 

f = open('Users/Lu/Desktop/Images','wb') # folder 
f.write(img) 
f.close()

但我没有得到任何结果或错误

我很确定代码中有些东西不起作用

请你看一下,告诉我怎么了


Tags: pathfromimportselfcomhttpurldownload
3条回答

这个html = urlopen(url)应该是html = urlopen(self.url)

编辑:您可以像这样获取URL

def scrape_images(self):
       html = urlopen(selfurl)
       bs4 = bs(html, 'html.parser')

       urls = []
       for img in bs4.find_all('img'):
           urls.append(img.attrs.get("src"))

       return urls

下一步就是找到如何下载它们

尝试以下代码下载图像。使用urlretrieve将图像src值下载到位置

from urllib.request import urlretrieve
import requests
from bs4 import BeautifulSoup
import os
url='http://www.photobirdireland.com/garden-birds.html'
data=requests.get(url).text
soup=BeautifulSoup(data,"html.parser")
images=['http://www.photobirdireland.com/'+ image['src'] for image in soup.find_all('img')]

for img in images:
    urlretrieve(img,os.path.basename(img)) 

您的代码不完整,在images = bs4.find_all('img', {})

示例

for image in images:
    # get the img url
    img_url = image.get('src').replace('\\', '/')
    real_url = "http://www.photobirdireland.com/" + img_url

    # get the image name
    img_name = str(img_url.split('/')[-1])

    # now download the image using - import urllib.request & import os
    print("downloading {}".format(img_url))
    urllib.request.urlretrieve(real_url, os.path.join(path, img_name))

完整的代码应该如下所示-

import os
import urllib.request
import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup as Bs


class ImageScraper:
    def __init__(self, url, download_path):
        self.url = url
        self.download_path = download_path
        self.session = requests.Session()

    def scrape_images(self):
        path = self.download_path
        html = urlopen(self.url)
        bs4 = Bs(html, 'html.parser')
        images = bs4.find_all('img', {})

        for image in images:
            # get the img url
            img_url = image.get('src').replace('\\', '/')
            real_url = "http://www.photobirdireland.com/" + img_url
            print(real_url)
            # get the image name
            img_name = str(img_url.split('/')[-1])
            print(img_name)
            print("downloading {}".format(img_url))
            urllib.request.urlretrieve(real_url, os.path.join(path, img_name))


scraper = ImageScraper(
    url="http://www.photobirdireland.com/garden-birds.html", download_path=r"D:\Temp\Images")
scraper.scrape_images()

相关问题 更多 >