在 Javascript 中从 Google 搜索中提取关键字 [英] Extract keyword from Google search in Javascript

查看:31
本文介绍了在 Javascript 中从 Google 搜索中提取关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Google 搜索的 url 中提取搜索关键字(例如,对于关键字car":http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=car&aq ...(这里的汽车在q="和&aq"之间但我注意到令牌可能会改变 ["&ie" 而不是 "&aq"]).

I'd like to extract from the url of a Google search the keyword of the search (e.g. for the keyword "car" : http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=car&aq ... (here car is between "q=" and "&aq" but I noticed that the tokens might change ["&ie" instead of "&aq"]).

我是 regex 和 Google 搜索的新手,到目前为止我还没有找到解决方案,请问有人知道怎么做吗?

Being new at regex and Google search I haven't found so far the solution, does someone know how to do this please ?

布鲁诺

推荐答案

您不需要正则表达式.修改自 http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html:

You don't need a regular expression for this. Modified from http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html:

function getUrlVars(href)
{
    var vars = [], hash;
    var hashes = href.slice(href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

然后你可以这样做:

var v = getUrlVars("http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=car&aq");
var q = v.q;

这篇关于在 Javascript 中从 Google 搜索中提取关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆