【问题标题】:How to download image from linkedin by using python?如何使用python从linkedin下载图像?
【发布时间】:2020-04-09 06:49:39
【问题描述】:

我正在尝试在 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】:

    这是我在 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)
    

    【讨论】:

    • 很高兴听到这个消息!请将其标记为答案,以便其他人也可以找到解决方案!
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多