o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
html文档如下所示:<div class="entry-content"><div…
- User
html文档如下所示:eBay published a long blog post about the popularity of vintage goods such as housewares and furniture – a reminder to collectors that Etsy isn’t the only source of vintage goods.Check out the full post, “How Vintage IKEA, Farberware, and Other Vintage Housewares Went from Disposable to Indispensable on eBay,” on the eBay corporate blog. 如何使用xpath获取class为entry-content的所有子节点,但是要排除前后id为dpsp-content开头的子节点
- Assistant
要使用 XPath 获取 class 为
entry-content的所有子节点,但排除 id 以dpsp-content开头的子节点,可以使用以下 XPath 表达式://div[@class='entry-content']/*[not(starts-with(@id, 'dpsp-content'))]//div[@class='entry-content']选取所有 class 为entry-content的div元素。*表示选取所有子元素。[not(starts-with(@id, 'dpsp-content'))]排除所有 id 以dpsp-content开头的元素。
示例代码:
from lxml import etree html_content = '''<div class="entry-content"> <div id="dpsp-content-top"> ... </div> <div class="wp-block-image"> ... </div> <p> ... </p> <p> ... </p> <div id="dpsp-content-bottom"> ... </div> </div>''' tree = etree.HTML(html_content) result = tree.xpath("//div[@class='entry-content']/*[not(starts-with(@id, 'dpsp-content'))]") for element in result: print(etree.tostring(element))输出:
<div class="wp-block-image"> ... </div> <p> ... </p> <p> ... </p>这样就成功获取了所需的子节点,排除了不需要的部分。
使用以下 XPath 表达式:
//div[@class='entry-content']/*[not(starts-with(@id, 'dpsp-content'))]