python xpath spaces

html_str = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page name</title>
</head>
<body>
    <div>
        div-text
        <span>span-text</span>
        <a>a-text</a>
        <p>p-text</p>
    </div>
    <table>
        <tr>
            <th>Heading</th>
            <th>Another Heading</th>
        </tr>
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        table-text-2
    </table>
</body>
</html>
"""

from lxml import etree
html = etree.HTML(html_str)
res = html.xpath('head/title/text()')
res = html.xpath('normalize-space(body/div/text())')
print(res)

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848013&siteId=291194637