首页 > 解决方案 > 无法使用 pixivpy3 从 Pixiv 下载图像

问题描述

我正在尝试使用该pixivpy3库从 Pixiv 下载图像。我能够进行身份验证并接收图像列表,但是在使用requests下载图像时,我收到了 403。

pic = random.choice(AllPics)
PicID = pic['id']
PicURL = f"https://www.pixiv.net/en/artworks/{PicID}"
SavedPicPath = f'imgTmp/{PicID}_p0.jpg'
response = requests.get(PicURL, 
                        headers={
                            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 
                            'referer' : PicURL,
                            'scheme' : 'https',
                            'accept' : 'image/webp,image/apng,image/*,*/*;q=0.8'
                            },
                        stream=True)
if os.path.exists(SavedPicPath):
    os.remove(SavedPicPath)
with open(SavedPicPath, 'wb') as Out_file:
    Out_file.write(response.raw)   
    DiscordFileObject = File(SavedPicPath)

我收到的错误是 TypeError: a bytes-like object is required, not 'HTTPResponse',这是因为response是 403 错误。我直接从 Chrome 复制了标题,所以我不确定为什么我仍然得到这个。我也认证了

标签: pythonpython-requests

解决方案


问题是我的网址不正确。正确的网址是pic['image_urls']['large']


推荐阅读