如何用python获取html标签内内容

 时间:2024-11-03 15:19:20

1、使用pip工具安装BeautifulSoup 模块,命令如下:Python3以上 使用:$ pip installBeautifulSoup4 python2.7 使用命令:$ pip installBeautifulSoup

如何用python获取html标签内内容

3、创建一段html文档字符串,代码如下:html_str= '''争犸禀淫<html> <head> <title>&造婷用痃lt;/title> </head> <body> <div class="nav">百度经验</div> </body></html>'''

如何用python获取html标签内内容

5、解析标签内容,代码如下:div =bs_xml.findAll('div',{'class':'nav'})div[0].contents

如何用python获取html标签内内容

6、全部代码:from bs4 import BeautifulSouphtml_str= '''<html> <head&爿讥旌护gt; <title></title> </head> <body> <div class="nav">百度经验</div> </body></html>'''bs_xml = BeautifulSoup(html_str)print(bs_xml.prettify())div =bs_xml.findAll('div',{'class':'nav'})div[0].contents

  • python如何使用文本框
  • 背词达人APP如何修改个人头像
  • VB6.0如何绘制Timer控件
  • VS下的项目位置不受信任时警告用户属性如何关闭
  • VB编写红绿灯切换小程序
  • 热门搜索
    怎么发海参 有眼袋怎么消除 眼睛流泪是怎么回事 念珠菌感染怎么治疗 桂龙药膏效果怎么样 薯片怎么做 职务职称怎么填 童谣怎么写 体虚多汗怎么办 卷发器怎么用