第一次使用requests获取网页源代码

时间:2020-05-08 23:26:34   收藏:0   阅读:92
  1. 安装第三方库
  2. pip install requests
  3. 导入requests,使用get()方法获取网页源代码
  4. 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)

     

  5. 第一行导入requests库
  6. 第二行使用get方式获得网页,得到一个Response对象。此时不能直接打印,否则出来的结果是
  7. <Response [200]>

     

  8. 第三行使用.content来显示bytes型网页的源代码
  9. 第四行将bytes解码为字符串类型的源代码
  10. 第五行打印字符串
  11. 代码简化后为
  1. 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
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!