python爬虫反爬--模拟客户请求

动态内容或反爬机制

百度首页可能根据你的请求头(如未设置 User-Agent)返回不同的内容,或者使用了动态加载技术。直接通过 requests.get() 获取的可能是简化版的 HTML。

方案: 添加请求头模拟浏览器

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers)
posted @ 2025-02-10 10:19  蔡姬小媛  阅读(26)  评论(0)    收藏  举报