栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

构建header在python爬虫中出错怎么解决?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

构建header在python爬虫中出错怎么解决?

最近频繁讲到header模块的知识,受到很多小伙伴们的询问,看来大家对获取新数据之类的资料需求量比较多,接下来小编也会着重于这方面的文章更新。今天主要讲的是之前使用常见user-agent方法出现的报错问题,出于python初学者处理此类问题会比较棘手,所以我们一起来学习一下吧。


想让host在user-agent的位置,该怎么办?如果不换的话,请求会报500的错误


解决:

import requests
 
Information = requests.Session()
print(Information.headers)
 
Information.headers.update({
    "Accept-Encoding": "identity",
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
    "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9; Redmi Note 5 MIUI/20.1.16)",
    "Connection": "Keep - Alive",
    "Accept": None
})
print(Information.headers)
 
Information.headers.clear()
Information.headers.update({
    "Accept-Encoding": "identity",
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
    "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9; Redmi Note 5 MIUI/20.1.16)",
    "Connection": "Keep - Alive",
    "Accept": None
})
print(Information.headers)
关键点在于 "Information.headers.clear()"


对于此类用user-agent在构建header出错的小伙伴,都可以试试小编的这种方法。看看能不能顺利解决,不行的话小编再跟大家一起寻求其他的方法。更多Python学习推荐:PyThon学习网教学中心。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/11804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号