【问题标题】:Identifying identical "Feeling Lucky" searches识别相同的“手气”搜索
【发布时间】:2018-11-07 09:53:23
【问题描述】:

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

我一直在尝试通过在 iframe 中同时搜索(12 和 12)然后获取 iframe 重定向到的 URL 来做到这一点,但由于它们位于不同的域中,我无法让它工作。有没有办法做到这一点?

此外,如果有更好的方法来做到这一点,那么使用感觉幸运搜索也可以。

【问题讨论】:

  • 我不清楚你想做什么。 “搜索”是“相同的”是什么意思。您从这些 URL 中检索什么?也许显示一些代码并用edit 解释更多关于您想要实现的目标。
  • 试图澄清我在问什么。

标签: javascript php html node.js


【解决方案1】:

由于安全限制,您的浏览器会将来自不同域的 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 来完成此操作,因为您已经在 Web 服务器上使用了它。我们使用两个页面,一个用于输入请求 URL 并使用结果,另一个用于执行 HTTP GET 请求。这是一些工作代码:

重要提示

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

比较index.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>

compare.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 请求,其中显示值。

【讨论】:

  • 我怀疑 iframe 可能在此处的其他帖子中存在该问题。这段代码完全符合我的要求,但是,我是 Node.js 的新手,我不知道如何在我的网站上运行它。我已经下载了节点,当它是 .js 文件时它可以工作,但是我的网站是 .php 并导致错误。您能否解释一下如何将其整合到我的网站中,或者为我指明正确的方向。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
相关资源
最近更新 更多