download image from url using python urllib but receiving HTTP Error 403: Forbidden

前端 未结 3 1643
广开言路
广开言路 2020-11-27 20:44

I want to download image file from a url using python module \"urllib.request\", which works for some website (e.g. mangastream.com), but does not work for another (mangadoo

3条回答
  •  一向
    一向 (楼主)
    2020-11-27 21:11

    This website is blocking the user-agent used by urllib, so you need to change it in your request. Unfortunately I don't think urlretrieve supports this directly.

    I advise for the use of the beautiful requests library, the code becomes (from here) :

    import requests
    import shutil
    
    r = requests.get('http://mangadoom.co/wp-content/manga/5170/886/005.png', stream=True)
    if r.status_code == 200:
        with open("img.png", 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    

    Note that it seems this website does not forbide requests user-agent. But if need to be modified it is easy :

    r = requests.get('http://mangadoom.co/wp-content/manga/5170/886/005.png',
                     stream=True, headers={'User-agent': 'Mozilla/5.0'})
    

    Also relevant : changing user-agent in urllib

提交回复
热议问题