第一次使用requests获取网页源代码
时间:2020-05-08 23:26:34
收藏:0
阅读:92
- 安装第三方库
- pip install requests
- 导入requests,使用get()方法获取网页源代码
-
1 import requests 2 html = requests .get (‘http://ttlike.top‘) 3 html_bytes = html.content 4 html_str = html_bytes.decode() 5 print (html_str)
- 第一行导入requests库
- 第二行使用get方式获得网页,得到一个Response对象。此时不能直接打印,否则出来的结果是
-
<Response [200]>
- 第三行使用.content来显示bytes型网页的源代码
- 第四行将bytes解码为字符串类型的源代码
- 第五行打印字符串
- 代码简化后为
-
1 import requests 2 html = requests .get (‘http://exercise.kingname.info/exercise_requests_get.html‘).content .decode() 3 print (html)
原文:https://www.cnblogs.com/hxlinux/p/12853020.html
评论(0)