爬虫中 Requests获取网页遇到问题的详解(¥51)

本文详细探讨了使用Requests库在爬虫过程中遇到的网页获取问题,重点在于Requests如何处理Accept-Encoding及内容解压。通过对比两种方式,解释了为何content.read()能正确显示内容,并说明了Requests通过HTTP头部和chardet.detect来猜测并解码响应的编码方式,建议使用response.content.decode()进行解码。
摘要由CSDN通过智能技术生成

#使用Requests获取新浪首页(两种对比)

import  requests
response = requests.get("http://www.sina.com")
print(response.request.headers)
print(response.content.decode())

结果:

{'User-Agent': 'python-requests/2.12.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<!DOCTYPE html>
<!-- [ published at 2017-06-09 15:15:23 ] -->
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>新浪首页</title>
    <meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
  ...
import  requests
response = requests.get("http://www.sina.com")
print(response.r
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值