user959129
user959129

Reputation:

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

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

Upvotes: 0

Views: 768

Answers (1)

Mike Pennington
Mike Pennington

Reputation: 43077

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

Upvotes: 1

Related Questions