Error when crawling and downloading an image

Asked 2 years ago, Updated 2 years ago, 107 views

1 Answers

import requests
from bs4 import BeautifulSoup as bs

def filesave(url):
    try:
        urlsplit = url.split('/')[-1]
        name = 'C:/Users/User/hi/'+urlsplit
        bn = requests.get(url).content
        if bn[0:3] != b'\xff\xd8\xff':
            print('this file is not JPEG file format')
            return 0
        else:
            if 'jpg' not in urlsplit:
                name += '.jpg'
        f = open(name,'wb')
        f.write(bn)
        f.close()
        print(f'[!] {name} saved')
        return name
    except Exception as e:
        print(e)
        return 0

def imgsrc(url):
    s = bs(requests.get(url).text, 'html.parser')
    img = s.find('div', {'class':'gd_imgArea'})
    if img is not None:
        return img.span.em.img['src']
    else:
        return None

url1 = 'http://www.yes24.com/Product/Goods/58397337'
url2 = 'http://www.yes24.com/Product/Goods/58412700'

filesave(imgsrc(url1))
filesave(imgsrc(url2))

Please keep that in mind You can skip identifying file signature.


2022-09-20 20:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.