Python requests爬虫豆瓣图片返回数据为空。

爬个豆瓣图片,记录个小问题,发现爬取豆瓣原图的时候拿不到数据,返回为空,爬小图可以,爬其他网站的也都正常,最后发现是header中If-Modified-Since这个参数的原因,加上了就拿不到数据,去掉就行。

python 复制代码
import requests

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9,ru;q=0.8,en;q=0.7,zh-TW;q=0.6',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    # 'If-Modified-Since': 'Sun, 03 Nov 2019 04:42:19 GMT',
    'Referer': 'https://movie.douban.com/celebrity/1010504/photo/2421104812/',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
response = requests.get('https://img2.doubanio.com/view/photo/raw/public/p2421104812.jpg', headers=headers)
content = response.content
with open('temp.jpg','wb') as f:
    f.write(content)
相关推荐
wuyk5553 小时前
第2章:六步换相原理全解+STM32工程实战
c语言·开发语言·stm32·单片机·嵌入式硬件
hey_sml3 小时前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
萧瑟余晖5 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
北斗落凡尘5 小时前
LangGraph 入门实战(2)
python·langchain
ttod_qzstudio5 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
萧瑟余晖7 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring
jufeng13077 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 1 篇】
人工智能·python·架构·agent
月光船幽幽7 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
二十雨辰9 小时前
[Java]-Spring面试题
java·开发语言
_oP_i10 小时前
python 后缀 mjs文件
开发语言·python