Python 并使用 lxml 查找使用 lxml html 的字符串的部分

发布于 2024-12-08 16:04:20 字数 368 浏览 0 评论 0原文

我有一个看起来像这样的选项卡...

<div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;">

我可以用来

doc = lxml.html.document_fromstring(html)
el = doc.get_element_by_id('contentDiv_reviewHistoPop_B004HYGU18_4792') 

查找标签,但是如何使用例如通配符来查找 contentDiv_reviewHistoPop* 来查找包含字符串一部分的标签?

谢谢

I have a tab that looks like this...

<div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;">

I can use

doc = lxml.html.document_fromstring(html)
el = doc.get_element_by_id('contentDiv_reviewHistoPop_B004HYGU18_4792') 

to find the tag but how do I use e.g. a wild card to find contentDiv_reviewHistoPop* that would find the tags that contains a portion of the string?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2024-12-15 16:04:20

您应该这样做...

import lxml.etree as ET
from lxml.etree import XMLParser

parser = XMLParser(ns_clean=True, recover=True)
html = """<html><body><div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;"></body></html>"""
tree = ET.fromstring(html, parser)
tmp = list()
for elem in tree.iter():
    if elem.tag == 'div':
        for ii in elem.items():
            if (ii[0].lower() == 'id') and ('contentDiv_reviewHistoPop' in ii[1]):
                tmp.append(elem)

tmp 将包含具有匹配文本的匹配 div 元素的列表。

如果您只能使用 lxml.html,那么请执行此操作...

import lxml.html

html = """<html><body><div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;"></body></html>"""
doc = lxml.html.document_fromstring(html)
tmp = list()
for elem in doc.iter():
    if elem.tag == 'div':
        for ii in elem.items():
            if (ii[0].lower() == 'id') and ('contentDiv_reviewHistoPop' in ii[1]):
                tmp.append(elem)

再次... tmp 将包含匹配的标签

You should do this...

import lxml.etree as ET
from lxml.etree import XMLParser

parser = XMLParser(ns_clean=True, recover=True)
html = """<html><body><div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;"></body></html>"""
tree = ET.fromstring(html, parser)
tmp = list()
for elem in tree.iter():
    if elem.tag == 'div':
        for ii in elem.items():
            if (ii[0].lower() == 'id') and ('contentDiv_reviewHistoPop' in ii[1]):
                tmp.append(elem)

tmp will contain the list of matching div elements with the matching text.

If you can only use lxml.html, then do this...

import lxml.html

html = """<html><body><div id="contentDiv_reviewHistoPop_B004HYGU18_4792" style="display:none;"></body></html>"""
doc = lxml.html.document_fromstring(html)
tmp = list()
for elem in doc.iter():
    if elem.tag == 'div':
        for ii in elem.items():
            if (ii[0].lower() == 'id') and ('contentDiv_reviewHistoPop' in ii[1]):
                tmp.append(elem)

Again... tmp will contain the matching tags

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文