一个简单的python获取html页面
版本说明:
Testing system os : Windows 7
Python : 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
安装模块:
1.requests模块安装
C:\Users\Administrator> pip install requests -i http://pypi.douban.com/simple
看到Successfully 就是安装成功了,参数-i是临时指定去那个网站找模块,国内也有其他源可以选择
2. lxml模块安装
C:\Users\Administrator>pip install lxml -i http://pypi.douban.com/simple
看到Successfully 就是安装成功了,参数-i是临时指定去那个网站找模块,国内也有其他源可以选择
获取html内容:
## 引入模块 > import requests > from lxml import etree ## 打开baidu.com网站 > r = requests.get('https://baidu.com') ## 汉字显示乱码 > print(r.text) ## 查看当前编码 > r.encoding ## 设置编码utf-8 ,因为网页是utf-8,编码不对显示汉字会乱码 > r.encondig = 'utf-8' ## 重新打印汉字就正常显示了 > print(r.text)
汉字显示乱码
设置编码后汉字显示正常
参考:
https://www.jb51.net/article/161053.htm
https://www.cnblogs.com/lanyinhao/p/9634742.html
AD:【腾讯云服务器大降价】2核4G 222元/3年 1核2G 38元/年
AD:【腾讯云服务器大降价】2核4G 222元/3年 1核2G 38元/年
AD:【腾讯云服务器大降价】2核4G 222元/3年 1核2G 38元/年
AD:【腾讯云服务器大降价】2核4G 222元/3年 1核2G 38元/年
本文来自一日成建站教程(https://www.yiricheng.cn/),为广大编程爱好者提供零基础快速搭建博客网站的方法,分享计算机网络、HTML、CSS,、JavaScript、PHP、响应式设计以及Python自动化办公的博客文章,各类小工具、电子书、免费教程。