javascript - 识别相同的 "Feeling Lucky"搜索

标签 javascript php html node.js

我正在尝试在我的网站中制作一个程序来测试不同搜索的热门搜索结果是否相同。例如,它应该表明“12”和“12”的顶部搜索结果相同,因为两者的顶部结果都是 https://en.wikipedia.org/wiki/12_(number)。 .使用谷歌的幸运搜索方法,它们都会重定向到同一页面,但我不知道如何获取重定向的 url 或它的内容来确定它们是否相同。

我一直试图通过在 iframe 中搜索(12 和 12)然后获取 iframe 重定向到的 URL 来实现这一点,但由于它们位于不同的域中,所以我无法正常工作。有办法做到这一点吗?

此外,如果有更好的方法来执行此操作,那么使用幸运搜索也可以。

最佳答案

由于安全限制,您的浏览器沙箱来自不同域的 iframe 以防止 XSS。有相当广泛的规则来防止任何此类事件,因为攻击者可以轻松加载敏感网站并从中抓取个人信息。即使在 JavaScript 中使用 GET 请求也会阻止您从跨域页面收集信息。


Node.js 方法

为了从 Google 搜索页面抓取数据,我会使用外部工具,例如 Node.jsNightwatch.js ,可用于轻松地自动执行网络任务,例如您希望完成的任务。

因为您只是想比较“手气不错”搜索的结果页面,所以您可以使用 Node.js request库来执行您的请求,并比较结果数据。这是一些工作代码:

var request = require("request");

var url1 = "https://www.google.com/search?hl=en&q=wikipediatwelve&btnI=I'm+Feeling+Lucky&aq=f&oq=";
var url2 = "https://www.google.com/search?hl=en&q=wikipedia&btnI=I'm+Feeling+Lucky&aq=f&oq=";

request(url1, function (error1, response1, body1) {
    request(url2, function (error2, response2, body2) {
        console.log(response1.request.uri.href); // https://en.wikipedia.org/wiki/12_(number)
        console.log(response2.request.uri.href); // https://en.wikipedia.org/wiki/Main_Page
        if(response1.request.uri.href == response2.request.uri.href){
            console.log("Same page!");
        }else{
            console.log("Different page!");
        }
    });
});

如果你的机器上没有安装 Node.js,你可以试试这段代码 here .只需单击页面底部的“克隆并编辑此文档”,然后注册/登录即可。

您还可以在其他平台(例如 Python 而不是 Node.js)中使用等效库。


PHP 方法

您也可以使用 PHP 完成此操作,因为您已经在您的网络服务器上使用它。我们使用两个页面,一个用于输入请求 URL 并使用结果,另一个用于执行 HTTP GET 请求。这是一些工作代码:

重要提示

如果您将这些 PHP 页面公开到互联网,任何人都可以使用您的网络服务器向任何 URL 发出 HTTP 请求。这是危险的,我强烈建议不要这样做。您需要添加检查以确保您的代码未被恶意使用。如果代码仅供您使用,并且绝对无法通过 Internet 访问,则这不适用。 Security through obscurity还不够好!

比较索引.php

<?php
    $sendLoc = "compare.php";
?>

<!-- This part submits the URLs to the compare script to get executed -->
<form action="<?php echo($sendLoc); ?>" method="post">
    <input type="text" name="URL1" placeholder="Enter URL1">
    <input type="text" name="URL2" placeholder="Enter URL2">
    <button type="submit">Submit</button>
</form>

<!-- This part gets the posted values back from the compare script to be processed in JavaScript -->
<script>
    var finalURL1 = "<?php echo($_POST['fURL1']); ?>"; // PHP will fill these variables if we just requested a comparison
    var finalURL2 = "<?php echo($_POST['fURL2']); ?>";

    document.write(finalURL1); //Just an example, displaying the returned values and if they're equal
    document.write("<br>");
    document.write(finalURL2);
    document.write("<br>");
    if(finalURL1 && finalURL2){
        document.write("Equal: " + (finalURL1==finalURL2));
    }
</script>

比较.php

<?php
    $returnLoc = "compareindex.php";
?>

<!-- This part gets the URL values posted and determines the final URLs (after redirect) -->
<?php
    function getRedirectURL($URL) {
        $ch = curl_init(); //Create curl resource 
        curl_setopt($ch, CURLOPT_URL, $URL); //Set starting url 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return the transfer as a string 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Follow redirects
        curl_exec($ch); //Execute request to get final url, discard data
        $fURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //Get final url
        curl_close($ch); //Close curl resource to free up system resources 
        return $fURL; //Return final url
    }

    $URL1 = $_POST['URL1'];
    $URL2 = $_POST['URL2'];

    $returnValues['fURL1'] = getRedirectURL($URL1);
    $returnValues['fURL2'] = getRedirectURL($URL2);
?>

<!-- This part takes the final URLs and posts them back to the original page -->
<form id="redirForm" action="<?php echo($returnLoc); ?>" method="post">
<?php
    foreach ($returnValues as $a => $b) { //Makes a HTML form input for each return value
        echo '<input type="hidden" name="'.htmlentities($a).'" value="'.htmlentities($b).'">';
    }
?>
</form>
<script>
    document.getElementById('redirForm').submit(); //Submit the form automatically
</script>

您在输入框中键入您的 URL,然后当您按下提交时,compareindex.phpcompare.php 发出 POST 请求。 compare.php 然后对发布的两个 URL 发出 GET 请求,然后使用重定向 URL 向 compareindex.php 发出 POST 请求,其中显示值。

Image of the result

关于javascript - 识别相同的 "Feeling Lucky"搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572548/

相关文章:

php - 在 php 中显示进度条直到页面加载?

javascript - 如何在网站上的语音合成中设置语言?

php - 使用 PHP 的数学游戏

html - div 列布局,具有最小宽度的可拉伸(stretch)列

php - 如何在另一个php文件中列出一个php文件的值

php - 在下拉列表中显示 mysql 记录的值

html - 在 Bootstrap 选项卡周围添加边框

javascript - JS 数组显示的用户个人资料图片不正确 - 如何解决这个问题?

javascript - 为什么 paper.js 路径创建在 iOS 设备上不起作用?

javascript - Mongoose 在循环内获取多个对象