Question

the following code parses the string.

import lxml.html

doc = lxml.html.document_fromstring("""<html>
 <body>
   <span class="simple_text">One</span> tehfghhxt</br>
   <span class="cyrillic_text">Второй</span> cyrcyrcyr</br>
 </body>
</html>
""")

txt1 = doc.xpath('/html/body/span[@class="simple_text"]/text()[1]')
print(txt1)

I would like to know how to parse the file that contains the same string. and which is in the same directory. please tell me.

Was it helpful?

Solution

string = open('file_with_string.html', 'r').read()
doc = lxml.html.document_fromstring(string)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top