Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden [英] Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden

查看:54
本文介绍了Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 urrlib 下载 GIF 文件,但它抛出此错误:

I am trying to download a GIF file with urrlib, but it is throwing this error:

urllib.error.HTTPError: HTTP Error 403: Forbidden

当我从其他博客站点下载时不会发生这种情况.这是我的代码:

This does not happen when I download from other blog sites. This is my code:

import requests
import urllib.request

url_1 = 'https://goodlogo.com/images/logos/small/nike_classic_logo_2355.gif'

source_code = requests.get(url_1,headers = {'User-Agent': 'Mozilla/5.0'})    

path = 'C:/Users/roysu/Desktop/src_code/Python_projects/python/web_scrap/myPath/'

full_name = path + ".gif"    
urllib.request.urlretrieve(url_1,full_name)

推荐答案

不要使用 urllib.request.urlretrieve.相反,像这样使用 requests 库:

Don't use urllib.request.urlretrieve. Instead, use the requests library like this:

import requests

url = 'https://goodlogo.com/images/logos/small/nike_classic_logo_2355.gif'

path = "D:\\Test.gif"

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})

file = open(path, "wb")

file.write(response.content)

file.close()

输出:

希望这会有所帮助!

这篇关于Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆