将import中的双斜杠替换为单斜杠.io XPath选择器



我正在使用import。抓取一些页面。我遇到一个页面,使用内部href像这样:http://domain.com//Event -注意域名后的双斜杠。根据我的研究,这是为了SEO目的而做的,但我需要得到没有那些双斜杠的url,所以它返回http://domain.com/Event

我正在尝试使用XPath(这是我很新的),我可以得到链接://a[contains(@class, 'event-info-btn')]//@href

我的下一步是尝试fn:repace()与此:fn:replace(//a[contains(@class, 'event-info-btn')]//@href, 'http://domain.com//', 'http://domain.com/')

我不确定我的实现是否很糟糕,或者是否导入。IO不支持这个

  • 我还将说明我尝试这样做的原因:import。IO在所有url上都失败了。如果我手动删除斜杠,然后再试一次,它工作得很好。

注意导入。io 声明支持XPath 2.0

你可能指的是/@href而不是//@href,但这不是真正的问题。

您的XPath正在返回href属性序列,而replace()期望一个字符串。

解决方案

对于这个HTML,

<div>
  <a class="event-info-btn" href="http://domain.com//1">one</a>
  <a class="event-info-btn" href="http://domain.com//2">one</a>
  <a class="event-info-btn" href="http://domain.com//3">one</a>
</div>
这个XPath

,

for $href in //a[contains(@class, 'event-info-btn')]/@href 
    return replace($href, 'http://domain.com//', 'http://domain.com/')

将返回

http://domain.com/1
http://domain.com/2
http://domain.com/3

要求。


更新

这在import中不起作用。我现在找不到一个类似小提琴的网站来测试它。

你可以在这里看到

导入。Io似乎只允许输入一行xpath。

您可以尝试将XPath放在一行中,然后:

for $href in //a[contains(@class, 'event-info-btn')]/@href return replace($href, 'http://domain.com//', 'http://domain.com/')

如果不起作用,则导入。io声称他们支持XPath 2.0是不正确的

最新更新