开发者

Python and using lxml to find part if a string using lxml html

开发者 https://www.devze.com 2023-04-11 19:46 出处:网络
I have a tab that looks like this... <div id=\"contentDiv_reviewHistoPop_B004HYGU18_4792\" style=\"display:none;\">

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* t开发者_JAVA百科hat would find the tags that contains a portion of the string?

Thanks


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号