Javascript “识别相同的”;“感到幸运”;搜查
我试图在我的网站上制作一个程序,测试不同搜索的顶级搜索结果是否相同。例如,它应该告诉您“12”和“12”的顶部搜索结果是相同的,因为两者的顶部结果都是相同的。使用google的feeling lucky搜索方法,它们都会重定向到同一个页面,但我不知道如何获取重定向的url或其内容,以确定它们是否相同 我一直试图通过在iframe中同时搜索(12和12),然后获取iframe重定向到的URL来实现这一点,但由于它们位于不同的域中,我无法实现这一点。有什么办法可以这样做吗Javascript “识别相同的”;“感到幸运”;搜查,javascript,php,html,node.js,Javascript,Php,Html,Node.js,我试图在我的网站上制作一个程序,测试不同搜索的顶级搜索结果是否相同。例如,它应该告诉您“12”和“12”的顶部搜索结果是相同的,因为两者的顶部结果都是相同的。使用google的feeling lucky搜索方法,它们都会重定向到同一个页面,但我不知道如何获取重定向的url或其内容,以确定它们是否相同 我一直试图通过在iframe中同时搜索(12和12),然后获取iframe重定向到的URL来实现这一点,但由于它们位于不同的域中,我无法实现这一点。有什么办法可以这样做吗 另外,如果有更好的方法,那
另外,如果有更好的方法,那么使用同样有效的feeling lucky搜索。由于安全限制,您的浏览器沙盒会从不同的域中删除iFrame以防止XSS。有相当广泛的规则可以防止任何类似的活动,因为攻击者可以轻松加载敏感网站并从中获取个人信息。即使在JavaScript中使用GET请求,也会阻止您从跨域页面收集信息
Node.js方法 对于从Google搜索页面中进行抓取,我会使用一个外部工具,如with,它可以用来轻松地自动化web任务,如您希望完成的任务 因为您只是尝试比较“我感觉很幸运”搜索的结果页面,所以可以使用Node.js库执行请求,并比较结果数据。下面是一些工作代码:
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,您可以使用此代码。只需单击页面底部的“克隆并编辑此文档”,然后注册/登录即可
您还可以在其他平台(如Python)中使用等效库,而不是Node.js
PHP方法 您也可以使用PHP来实现这一点,因为您已经在web服务器上使用它了。我们使用两个页面,一个用于输入请求URL并使用结果,另一个用于执行HTTP GET请求。下面是一些工作代码:
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!");
}
});
});
重要提示
如果将这些PHP页面公开到internet,任何人都可以使用web服务器向任何URL发出HTTP请求。这是危险的,我强烈建议不要这样做。您需要添加检查以确保您的代码没有被恶意使用。如果代码仅由您使用,并且在internet上绝对无法访问,则此项不适用。这还不够好
compareindex.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
$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检索什么?可能会展示一些代码,并对您想要实现的目标进行更多的解释。试图澄清我的问题。我怀疑iframes可能在这里的其他帖子中有这个问题。这段代码正是我想要的,但是,我是Node.js的新手,我不知道如何在我的网站上运行这段代码。我已经下载了node,当它是一个.js文件时,它就可以工作了,但是我的网站是.php,导致了一个错误。你能解释一下如何将其整合到我的网站中,或者为我指出正确的方向。