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)
相关推荐
dddaidai1236 分钟前
深入JVM(四):垃圾收集器
java·开发语言·jvm
test管家10 分钟前
PyTorch动态图编程与自定义网络层实战教程
python
AI科技星17 分钟前
圆柱螺旋运动方程的一步步求导与实验数据验证
开发语言·数据结构·经验分享·线性代数·算法·数学建模
laocooon52385788622 分钟前
python 收发信的功能。
开发语言·python
xixixi7777723 分钟前
STIX/TAXII:网络威胁情报的“普通话”与“顺丰快递”
开发语言·安全·php·威胁·攻击检测·stix·taxii
Tony Bai26 分钟前
Cloudflare 2025 年度报告发布——Go 语言再次“屠榜”API 领域,AI 流量激增!
开发语言·人工智能·后端·golang
ID_1800790547326 分钟前
有没有其他语言实现淘宝商品详情API接口采集的方案?
开发语言
清水白石00829 分钟前
《Python 责任链模式实战指南:从设计思想到工程落地》
开发语言·python·责任链模式
沛沛老爹36 分钟前
Web开发者快速上手AI Agent:基于LangChain的提示词应用优化实战
人工智能·python·langchain·提示词·rag·web转型
宁大小白36 分钟前
pythonstudy Day39
python·机器学习