无法从 LXML 获取 <a> 标记

Can't get <a> tag from LXML

我正在使用 Python 抓取城市词典以获取 word/phrase 的顶级定义、示例、作者和 like/dislike。我正在使用 lxml 访问站点并提取 xml 数据。我继续调用 XPath 数据请求并将其存储在树中。然而,当它 return 时,它并没有 return 预期的结果。

import os
from lxml import html
import requests

page = requests.get("https://www.urbandictionary.com/define.php?term=Food")
tree = html.fromstring(page.content)

# relative XPath to the definition
example = tree.xpath('//*[@id="content"]/div[1]/div[3]')

print(example)

out >> [' that has ever ', ' to ', '.']

它会跳过一些单词,即有标签的单词。 我正在寻找 return:The best thing that has ever happened to earth.,或者 ['The best thing ', 'that has ever ', 'happened ', 'to ', 'earth', '.']

我真的不在乎它是array/list形式还是字符串形式,我只想要lxml包含return中标签下的单词,但是它会那样做。我将如何获取内容?

提前致谢

这样试试:

example = tree.xpath('//*[@id="content"]//div[@class="meaning"]')
print(example[0].text_content())

输出:

The best thing that has ever happened to earth.

要获取所有定义,请将其更改为:

example = tree.xpath('//div[@class="meaning"]')
for ex in example:
    print(ex.text_content())

输出:

The best thing that has ever happened to earth.
when you've done something so cringe you can't stop replaying it in your head and it stops you from getting on with your every day life
the solution to all of women's problems.
The best thing ever
a substance you eat,then poop out.usually followed my a nap.
A basic human right that is restricted to stores and restaurants. If you try to steal food for your starving family you'll be locked away.
Food: as in what models dont eat