知道创宇爬虫设计第六天:requests
从实验室回学校之后,仅仅是换了个网络,却突然出现了乱码问题
折腾了半天编码无果,无意中发现把urllib2
换成requests
就好了
按照requests
官方文档里的解释
When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text.
难怪总在知乎看见人安利,确实是更好用一些啊。。。
具体用法也很简单方便
不过使用request.text
返回结果时,标题依然会乱码
You can also access the response body as bytes, for non-text requests
改成使用request.content
,果然一切正常了
代码
|