简体   繁体   中英

lxml findall div and span tags

如何找到保留订单的所有div和span标签。使用BeautifulSoup非常简单: soup.findAll(name=['span', 'div']) ,但我最近切换到lxml,因为它比BeautifulSoup快得多。

import lxml.html as LH
content = '''\
<tr>
<div>idend</div>
<span>Green<\span>
<tr>
'''
root = LH.fromstring(content)
for tag in root.xpath('//*[self::div or self::span]'):
    print(tag)

yields

<Element div at 0xb751f23c>
<Element span at 0xb751f11c>
import lxml.html
from lxml.cssselect import CSSSelector
content = result.read()
page_html = lxml.html.fromstring(content)

elements = page_html.xpath('//*[self::div or self::span]')

or

sd_selector = CSSSelector('span,div')
elements = sd_selector(page_html)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM