如何使用 lxml 和请求获取锚点中的元素文本?
How to get element text in an anchor using lxml and requests?
我正在研究 python 请求,现在我需要知道如何使用 xpath
.
解析 html
我在 https://www.tibia.com/community/?subtopic=worlds
从 Antica 那里得到了 xpath
这就是我现在的代码。
r = requests.get(U'https://www.tibia.com/community/?subtopic=worlds')
tree = html.fromstring(r.content)
worlds = tree.xpath('/html/body/div[3]/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/div[5]/div/div/div/table/tbody/tr/td/div/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[2]/td[1]/a/text()')
print(worlds)
我试图取回文本"Antica"
,但是 xpath
函数 returns 对我来说是一个空列表。
我会推荐使用美丽的汤,因为它更容易。
无论如何,如果您仍然希望使用 lxml,您可以尝试类似
r = requests.get(U'https://www.tibia.com/community/?subtopic=worlds')
tree = html.fromstring(r.content)
all_worlds = tree.xpath('/html/body//tr/td/a/@href')
for url in all_worlds:
print(url)
我正在研究 python 请求,现在我需要知道如何使用 xpath
.
html
我在 https://www.tibia.com/community/?subtopic=worlds
从 Antica 那里得到了xpath
这就是我现在的代码。
r = requests.get(U'https://www.tibia.com/community/?subtopic=worlds')
tree = html.fromstring(r.content)
worlds = tree.xpath('/html/body/div[3]/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/div[5]/div/div/div/table/tbody/tr/td/div/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[2]/td[1]/a/text()')
print(worlds)
我试图取回文本"Antica"
,但是 xpath
函数 returns 对我来说是一个空列表。
我会推荐使用美丽的汤,因为它更容易。
无论如何,如果您仍然希望使用 lxml,您可以尝试类似
r = requests.get(U'https://www.tibia.com/community/?subtopic=worlds')
tree = html.fromstring(r.content)
all_worlds = tree.xpath('/html/body//tr/td/a/@href')
for url in all_worlds:
print(url)