【问题标题】:How to extract the href within the text using lxml xpath and requests in python如何使用lxml xpath和python中的请求提取文本中的href
【发布时间】:2017-08-05 00:16:19
【问题描述】:

首先,我对python比较陌生。我需要从网页中的文本中提取链接,我正在使用带有 Python 3.5 的 lxml,但我无法弄清楚。这是我目前所拥有的:

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)。

我有点迷路了。提前致谢。

【问题讨论】:

    标签: python python-3.x text href lxml


    【解决方案1】:

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

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

    还是谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2018-08-31
      相关资源
      最近更新 更多