Domanda

il codice seguente analizza la stringa.

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)

Vorrei sapere come analizzare il file che contiene la stessa stringa.e che si trova nella stessa directory.ti prego, dimmelo.

È stato utile?

Soluzione

string = open('file_with_string.html', 'r').read()
doc = lxml.html.document_fromstring(string)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top