如何使用python从Linkedin下载图像?

问题描述 投票:2回答:1

我正在尝试通过在python中使用urllib下载公司徽标。但是每个图像的src是一个链接,该链接不以.jpg或.jpeg或任何其他图像文件格式结尾,例如:https://media.licdn.com/dms/image/C4E0BAQFV8hEDyrhygA/company-logo_100_100/0?e=1584576000&v=beta&t=rCeGYSP-rfRqEnSRiZszB3F1Bcks7kyXTYoiGrqUPTg。我为此使用以下代码:

imageSrc = companySoup.find('img', {'class': 'org-top-card-primary-content__logo Elevation-0dp lazy-image loaded ember-view'})
        imageSrcUrl = imageSrc['src'].strip()
        print(imageSrcUrl)
        urllib.urlretrieve(imageSrcUrl, os.path.basename(imageSrcUrl))

[请帮助我,如何在python中下载它。

python beautifulsoup linkedin urllib
1个回答
0
投票

这是我在pygame中使用的。但是您可以对其进行修改,以使用所需的任何内容(例如PIL?)

import requests
import io
import pygame
import traceback

def loadURLImage(url, headers='', timeout=10):

    image = None

    try:
        with requests.get(url, headers=headers, timeout=timeout) as pic:
            image_str = pic.content
            image_file = io.BytesIO(image_str)
            # Use .convert_alpha() if you find troubles with transparency
            image = pygame.image.load(image_file)
    except:
        print("Error getting image from URL", url)
        print(traceback.format_exc())

    return image

现在您可以使用以下方法将其保存到磁盘(BMP,TGA,PNG或JPEG):

file_name = "test.jpeg"    
pygame.image.save(image, file_name)
© www.soinside.com 2019 - 2024. All rights reserved.