【问题标题】:URL semantics analysis in R [closed]R中的URL语义分析[关闭]
【发布时间】:2016-05-15 10:53:38
【问题描述】:

我有一个包含各种 url 的数据集。

https://www.thetrainline.com/buytickets/combinedmatrix.aspx?Command=TimeTable
https://wwf-fb.zyngawithfriends.com/wwf-fb.a84485c126e67ea2787c.html
http://www.thetrainline.com/destinations/trains-to-london

我想做一个url的语义分析(/之后的url中的关键字)。

请帮帮我。

谢谢

【问题讨论】:

  • 在这种情况下什么是“语义分析”?哪个斜线?你尝试了什么?
  • 就像在第三个 url 中一样,我需要从 url 中提取目的地和到伦敦的火车。我对正则表达式的概念不是很熟悉。
  • / 后面的意思是指 URL 的路径吗?
  • 类似的东西? gsub('^(?:[^/]*/){3}','/', 'http://www.thetrainline.com/destinations/trains-to-london')
  • 是的。但它可以推广到所有 url 吗?

标签: r semantics text-analysis


【解决方案1】:

这比您手动完成要快得多,也更全面。

library(urltools)

URLs <- c("https://www.thetrainline.com/buytickets/combinedmatrix.aspx?Command=TimeTable",
          "https://wwf-fb.zyngawithfriends.com/wwf-fb.a84485c126e67ea2787c.html",
          "https:/test.com/thing.php?a=1&b=2",
          "http://www.thetrainline.com/destinations/trains-to-london")

url_parse(URLs)

##   scheme                      domain port                             path         parameter fragment
## 1  https        www.thetrainline.com        buytickets/combinedmatrix.aspx command=timetable         
## 2  https wwf-fb.zyngawithfriends.com      wwf-fb.a84485c126e67ea2787c.html                           
## 3                              https                    test.com/thing.php           a=1&b=2         
## 4   http        www.thetrainline.com         destinations/trains-to-london   

【讨论】:

  • 如果我们有不规则的恶意网址怎么办?即(http://.something-bad.com/something/whatever.com/https://… 它仍然可以识别不同的部分吗?(域、路径等)
  • 另外,您的第三个 URL 未正确解析...是吗?...哦,您忘记了 /
  • 如果你尝试解析这个:url2 &lt;- c('http://https://gallery46.co.il/wp-content/themes/twentytwelve/js/cj/1.html?http://www.freefilefillableforms.com + ') 它将失败。
  • @Sotos 因为它是一个无效的网址。欢迎请求请求。
  • 您希望无效 URL 的行为应该是……? pkg 适用于 https URL。也试试httr::parse_url
【解决方案2】:
URLs1 <- c('http://www.thetrainline.com/destinations/trains-to-london', 'https://wwf-fb.zyngawithfriends.com/wwf-fb.a84485c126e67ea2787c.html', 'https://www.thetrainline.com/buytickets/combinedmatrix.aspx?Command=TimeTable')
> gsub('^(?:[^/]*/){3}','/', URLs1)
[1] "/destinations/trains-to-london"                    "/wwf-fb.a84485c126e67ea2787c.html"                
[3] "/buytickets/combinedmatrix.aspx?Command=TimeTable"
> 

【讨论】:

  • 谢谢。我缺乏正则表达式的知识
猜你喜欢
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2012-06-04
  • 2010-11-27
  • 2016-06-01
相关资源
最近更新 更多