【问题标题】:R need to replace text between slashes in URLR需要替换URL中斜杠之间的文本
【发布时间】:2020-08-21 14:34:50
【问题描述】:

我正在尝试替换 URL 中的数字以提高图像质量,但不知道如何正确转义 / 以使其正常工作。

我需要更改://contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/1/ 到://contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/10/

当然,https 在这两个 URL 之前,但如果我包含它,论坛会将其转换为图像。

【问题讨论】:

  • sub("\\d+/?$", "10/", myurl)
  • 谢谢!那行得通:D

标签: r web web-scraping


【解决方案1】:

我在an online snippet 上尝试了下面的代码,效果很好。也使用了Aurèle 的提示。也许它会给你一个提示:

url <- "//contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/1/"
    
cat("Simple way\n")
simple <- gsub("/1/","/10/",url)
cat("Before: ", url, "\n")
cat("After: ", simple, "\n")
    
cat("Regex \n") As sugested by Aurèle
regex <- sub("\\d+/?$", "10/", url) 
cat("Before: ", url, "\n")
cat("After: ", regex, "\n")

【讨论】:

    【解决方案2】:

    Aurèle 的工作解决方案:

    sub("\d+/?$", "10/", myurl)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-12
      • 2020-01-17
      • 1970-01-01
      • 2015-03-26
      • 2015-02-14
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多