regex - 如何从 Google 搜索结果页面 URL 中提取关键字?

标签 regex r url

我的数据集中的变量之一包含 Google 搜索结果页面的 URL。我想从这些 URL 中提取搜索关键字。

示例数据集:

keyw <- structure(list(user = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("p1", "p2"), class = "factor"),
                   url = structure(c(3L, 5L, 4L, 1L, 2L, 6L), .Label = c("https://www.google.nl/search?q=five+fingers&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=kERoVbmMO6fp7AaGioCYAw", "https://www.google.nl/search?q=five+fingers&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=kERoVbmMO6fp7AaGioCYAw#safe=off&q=five+short+fingers+", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg#safe=off&q=high+five+with+a+chair", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg#safe=off&q=high+five+with+handshake", "https://www.youtube.com/watch?v=6HOallAdtDI"), class = "factor")), 
              .Names = c("user", "url"), class = "data.frame", row.names = c(NA, -6L))

到目前为止,我能够从 URL 中提取搜索关键字部分:
keyw$words <- sapply(str_extract_all(keyw$url, 'q=([^&#]*)'),paste, collapse=",")

但是,这仍然没有给我想要的结果。上面的代码给出了以下结果:
> keyw$words
[1] "q=high+five"                           
[2] "q=high+five,q=high+five+with+handshake"
[3] "q=high+five,q=high+five+with+a+chair"  
[4] "q=five+fingers"                        
[5] "q=five+fingers,q=five+short+fingers+"  
[6] ""                                      

此输出存在三个问题:
  • 我只需要单词作为字符串。而不是 q=high+five , 我需要 high,five .
  • 如第 2、3 和 5 行所示,URL 有时包含两部分和搜索关键字。由于第一部分只是对先前搜索的引用,因此我只需要第二个搜索查询。
  • 当 URL 不是 Google 搜索页面 URL 时,它应该返回 NA .

  • 想要的结果应该是:
    > keyw$words
    [1] "high,five"                           
    [2] "high,five,with,handshake"
    [3] "high,five,with,a,chair"  
    [4] "five,fingers"                        
    [5] "five,short,fingers"
    [6] NA
    

    我该如何解决这个问题?

    最佳答案

    评论后的另一个更新(看起来太复杂了,但这是我目前能做到的最好的 :)):

    keyw$words <- sapply(str_extract_all(str_extract(keyw$url,"https?:[/]{2}[^/]*google.*[/].*"),'(?<=q=|[+])([^$+#&]+)(?!.*q=)'),function(x) if(!length(x)) NA else paste(x,collapse=","))
    > keyw$words
    [1] "high,five"                "high,five,with,handshake" "high,five,with,a,chair"   "five,fingers"            
    [5] "five,short,fingers"       NA             
    

    变化是输入到 str_extract_all 的过滤器,从完整向量更改为“过滤”以匹配正则表达式,任何正则表达式都可以去那里以或多或少地精确匹配您想要的。

    这里的正则表达式是:
  • http http
  • s? 0 或 1 秒
  • [/]{2}正好是两个斜杠(使用字符类避免需要丑陋的 \\/ 结构并使事情更具可读性
  • [^/]*任意数量的非斜杠字符
  • google.*[/]匹配litteraly google,然后是任何东西到最后/
  • .*最后在最后一个斜杠后匹配或不匹配

  • 将 * 替换为 + 以确保存在参数( + 将要求前面的字符至少出现一次)

    受@BrodieG 启发的更新,如果没有匹配将返回 NA,但如果有 q= 仍将匹配任何站点在参数中。

    还是用同样的方法:
    > keyw$words <- sapply(str_extract_all(keyw$url,'(?:(?<=q=|\\+)([^$+#&]+)(?!.*q=))'),function(x) if(!length(x)) NA else paste(x,collapse=","))
    > keyw$words
    [1] "high,five"                "high,five,with,handshake" "high,five,with,a,chair"  
    [4] "five,fingers"             "five,short,fingers"       NA         
    

    Regex demo

    (后视 (?<=) 确保在单词之前的某处有 q= 或 +,负前瞻 (?!) 确保我们在行尾之前找不到 q=。

    字符类不允许 + 号停在每个单词上。

    关于regex - 如何从 Google 搜索结果页面 URL 中提取关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530616/

    相关文章:

    regex - 正则表达式在输入中的任意位置查找两个字符串

    javascript - 从 url 查询字符串中获取特定值

    html - 资源相对路径格式

    python - Python 中的 JSON.loads() ValueError 额外数据

    python - 将大文本文件拆分为句子

    R将大型CSV文件转换为HDFS

    R 到 BigQuery 数据上传错误

    r - 在R中的多列上执行lm()和segmented()

    Java - 从另一个类输入字符串时出现 MalformedURLException,但在粘贴相同的字符串时不会出现异常

    JavaScript 正则表达式