当前位置:   article > 正文

Python requests模块的学习_doctype html requests.get

doctype html requests.get

学习环境:python 2.7 windows10
一、 requests get 请求
1.获得一个get请求

r = requests.get("http://www.hactcm.edu.cn"
  • 1

2.获得网页文本

print r.text 
  • 1
输出结果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><title>河南中医药大学中文网</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><link rel="stylesheet" type="text/css" href="style/style.css">
<style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.可以看到乱码。打印requests获得的网页编码

print r.encoding
  • 1

输出结果是

ISO-8859-1
  • 1

4.可以知道正确编码未获得可以手工指定编码

r.encoding='utf-8'
  • 1

5.重新获得网页文本

print r.text
  • 1

输处的网页文本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><title>河南中医药大学中文网</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><link rel="stylesheet" type="text/css" href="style/style.css">
<
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/894085
推荐阅读
相关标签
  

闽ICP备14008679号