(python)Xpath如何提取html标签(HTML标签和内容) |
您所在的位置:网站首页 › 微博图片中的标签怎么弄的 › (python)Xpath如何提取html标签(HTML标签和内容) |
问题: (python)Xpath如何提取html标签(HTML标签和内容) 描述: Row value 1 Row value 2 Row value 3 Row value 4 Row value 1 Row value 1如何把table标签提取出来,结果如下: Row value 1 Row value 2 Row value 3 Row value 4 Row value 1 Row value 1代码如下: selector = etree.HTML(html) content = selector.xpath('//div/table')[0] print(content) # # 即:如何将Element对象转成str类型解决方案1: BeautifulSoup的find解决方案2: from lxml.html import fromstring, tostring # fromstring返回一个HtmlElement对象 # selector = fromstring(html) selector = etree.HTML(html) content = selector.xpath('//div/table')[0] print(content) # tostring方法即可返回原始html标签 original_html = tostring(content)解决方案3: [div/table]就行吧貌似解决方案4 from lxml import etree div = etree.HTML(html) table = div.xpath('//div/table')[0] content = etree.tostring(table,print_pretty=True, method='html') # 转为字符串以上介绍了“ (python)Xpath如何提取html标签(HTML标签和内容)”的问题解答,希望对有需要的网友有所帮助。 本文网址链接:http://www.codes51.com/itwd/4510100.html |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |