python3.x - python、requests get知乎报500
本文介绍了python3.x - python、requests get知乎报500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
在终端中
import requests as rq
result = rq.get("https://www.zhihu.com")
返回500错误,网页可以正常访问
另外 模拟登陆,返回登陆成功 但rq.get('http://zhihu.com') 也还是会报500。求解
解决方案
嘿嘿,因为知乎想防爬虫,而你直接这样去get的话,user agent会是"python...",知乎直接就挡掉了。但这种防爬虫太好破,你只要伪装一把user agent就行:
>>> import requests as rq
>>> result = rq.get("https://www.zhihu.com", headers = { 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20' })
>>> result
<Response [200]>
当然了,知乎这样做只是防小白爬虫,他还有各种后招去对付伪装user agent等更高级别的爬虫
这篇关于python3.x - python、requests get知乎报500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文