由于某种原因,urllib 在公共网站上不断给我错误 403。我的代码:
#imports
import requests
import os
import urllib.request
#setup
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
url = 'https://randomfox.ca/images/'
#extract filename
image_url = fox['image']
filename = os.path.basename(image_url)
#code
print(filename)
print(image_url)
urllib.request.urlretrieve(image_url, filename)
我不明白为什么这一直给我403,因为每当我将
image_url
粘贴到谷歌时,我就可以很好地去那里。
image_url 的示例是 https://randomfox.ca/images/58.jpg
我通过将
urllib.request.urlretrieve(image_url, filename)
替换为 \ 来修复它
with open(filename, 'wb') as f:
f.write(requests.get(image_url).content)