0

私は小さなプロジェクトに取り組んでいますが、この時点で立ち往生しているようです。うまくいけば、あなたの素晴らしい人々の何人かがこれについて私を助けてくれるでしょう.

検索エンジンの結果のページからランダムな単語または単語を選択する簡単で効率的な方法を見つけようとしています。これは私が立ち往生している部分です。

それを選んだら、単語を変数に格納します。

検索結果は次のようになります: http://i54.tinypic.com/34fllw1.png

前もって感謝します。ヒント/ヘルプをいただければ幸いです。

編集:ランダムな長さの連続した単語の文字列を選択できる方法はありますか?

4

3 に答える 3

1

google.com で動作する例を次に示します。

//get the text
var text=document.getElementById('rso').textContent;
  //find the words 
var words=text.match(/\b([a-z]{3,})\b/gi);
  //pick a word
alert(words[Math.floor(words.length*Math.random())]);

検索結果は、ID「rso」を持つ要素にリストされます。
正規表現は、少なくとも 3 文字 a ~ z で構成される文字列に一致します

于 2011-07-13T23:37:25.987 に答える
1

Google が説明に使用するクラスはstであるため、Dr.Molle のソリューションを改善したものを次に示します。

//get the text
var text=document.querySelector(".st"),output=[];
//loop through the descriptions
for(var i=0;i<text.length;i++){
    //find the words
    var words=text[i].innerText.match(/\b([a-z]{3,})\b/gi);
    //push the array to the output variable
    output.push.apply(output,words);
}
//pick a word
var theWord=output[Math.floor(Math.random()*output.length)];
//now theWord has a randomly chosen word
alert(theWord);

そして、いくつかの言葉を選ぶには:

//change the 10 to the max number of words
var amount=Math.floor(Math.random()*10),theWords="";
    //get the text
var text=document.querySelector(".st"),output=[];
//loop through the descriptions
for(var i=0;i<text.length;i++){
    //find the words
    var words=text[i].innerText.match(/\b([a-z]{3,})\b/gi);
    //push the array to the output variable
    output.push.apply(output,words);
}
//pick a word
var theNumber=Math.floor(Math.random()*output.length);
//loops the code the number of times randomly chosen
for(var i=0;i<amount;i++){
    //add on to the string
    theWords+=(i==0?"":" ")+output[theNumber+i];
}
//now theWords has a random number of consecutive words
alert(theWords);

Ad@m

于 2011-07-13T23:49:41.817 に答える
0

これは、説明から任意の数のランダムな単語を取得し、さまざまなGoogle検索ページを少し適切に処理する完全なGreasemonkeyスクリプトです(ページの取得方法に応じて、DOMがわずかに変更されます)。

// ==UserScript==
// @name            _RandomWord from results
// @include         http://www.google.com/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

function GrabRandomWords () {
    /*--- Google search results are wrapped in a list with the id "rso".
        Here, we want just the descriptions in the results, so we know
        that title and link tag tags can be excluded.
        (Alas, Google's search results pages vary quite a bit in the
        detailed structure of their HTML.)
    */
    var descriptText    = $("#rso li *:not(a,h1,h2,h3)").text ();

    //--- Split into words.  Change the "{1,}", if short words like "a" are to be excluded.
    var words           = descriptText.match (/\b(\w{1,})\b/g);

    //--- Pick 5 random words and store them in an array.
    if (words) {
        var ourRandWords    = [];
        for (var J = 0;  J < 5;  ++J)
            ourRandWords.push ( words[ Math.floor (words.length * Math.random() ) ] );

        alert (ourRandWords);
    }
}

//--- We must wait until page has fully loaded, because the results are usually Ajaxed in.
window.addEventListener ("load",
    function () {
        /*--- Page is "loaded", but results are still not in, still need a short delay.
            Note that listening for DOMSubtreeModified doesn't seem to work well.
        */
        setTimeout (function() { GrabRandomWords (); }, 666);
    },
    false
);
于 2011-07-14T00:50:47.917 に答える