当前位置:首页 » python教程 » 正文

python3中编码获取网页的实例方法

看: 1067次  时间:2020-11-21  分类 : python教程

学了python后,之前一些我们常用的方法,也可以换一种思路用python中的知识来解决。相信操作出来后,能收获一大批小粉丝们。就像我们没学习编程之前,看到那种大神都是可望而不可即。今天我们就之前简单获取网页的这种操作用python中的编码来解决,大家可以自行体会一下两者的不同。

1. encoding和apparent_encoding

import scrapy
url="https://www.xxx.net/html/gndy/dyzz/index.html"
re=requests.get(url)
#获取响应头Content-Type的charset值,有的网站没有charset字段,就可能使用默认的 ISO-8859-1
print(re.encoding)
#apparent_encoding就是获取网站真实的编码
print(re.apparent_encoding)

2. 处理方案

直接用r.encoding = ‘xxx'

re.encoding='utf-8'

3. requests的text() 跟 content() 有什么区别

re.text返回的是处理过的Unicode型的数据,

而使用re.content返回的是bytes型的原始数据。

4. 爬虫拿到的HTML和浏览器中的源码不相同时

通过下载源码对比

import requests
url = 'https://www.xxx.net/html/gndy/dyzz/index.html'
r = requests.get(url)
r.encoding = r.apparent_encoding
html = r.text
with open('test.html','w',encoding='utf8') as f:
f.write(html)

 

到此这篇关于python3中编码获取网页的实例方法的文章就介绍到这了,更多相关python3中编码如何获取网页内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

标签:scrapy  requests  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!