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)
相关推荐
雪度娃娃2 分钟前
设计模式——单例模式
开发语言·c++·设计模式
Cyber4K3 分钟前
【Python专项】进阶语法-日志分类与分析(2)
开发语言·前端·python
lbb 小魔仙7 分钟前
Python + LangChain 环境搭建完全指南:从零构建本地 RAG 知识库(附 Ollama 本地模型集成)
开发语言·python·langchain
风落无尘7 分钟前
Python 包发布全流程:从项目结构到 PyPI 上线,以及我踩过的那些坑
开发语言·python·pip
xxjj998a8 分钟前
PHP vs C#:两大编程语言终极对比
开发语言·c#·php
Lenyiin10 分钟前
《LeetCode 顺序刷题》61 - 70
java·c++·python·算法·leetcode·lenyiin
岁岁的O泡奶12 分钟前
NSSCTF_crypto_[LitCTF 2023]babyLCG
经验分享·python·算法·密码学·crypto·流密码
风落无尘16 分钟前
我用 LangChain 写了一个带“定速巡航”的向量化工具,发布到 PyPI 了!
人工智能·python·langchain
AI技术控16 分钟前
RAG 效果差不是模型问题:10 个检索增强失败原因总结
人工智能·python·自然语言处理
敲代码的瓦龙19 分钟前
Android?基础UI控件!!!
java·开发语言