簡體   English   中英

如何使用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函數向我返回了一個空列表。

我建議使用美麗的湯,因為它容易得多。

無論如何,如果您仍然希望使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM