簡體   English   中英

如何使用lxml xpath和python中的請求在文本中提取href

[英]How to extract the href within the text using lxml xpath and requests in python

首先,我是python的新手。 我需要從網頁中的文本中提取一個鏈接,我將lxml與Python 3.5結合使用,但我無法弄清楚。 這是我到目前為止的內容:

url = someUrl
page = requests.get(url)
webpage = html.fromstring(page.content)
fulllinks = webpage.xpath('//a/@href')
fulltext = webpage.xpath('//a/text()')


for line in fulltext:
    if line.startswith("SomethingHere"):
    'get the link from SomethingHere and do other stuff'

其中"somethingHere"是文本,我想要該文本的鏈接(例如www.someweb.com.br/trends )。

我有點迷路了。 提前致謝。

得到了我想要的東西。 答案是:

webpage.xpath("//a[starts-with(text(),'SomethingHere')]/@href")

不管怎么說,還是要謝謝你。

暫無
暫無

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

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