我正在尝试从Google的新的有趣事实功能中提取事实。如果您在Google中搜索有趣的事实,则会得到一个问题和一个答案。我想将一堆这些事实存储在以后使用。
我尝试使用javascript提取有趣的事实所在的div。但是,Google的div是动态的,每次搜索时都会更改。如果我尝试使用API或只是尝试使用https://www.google.com/search?q=fun+facts执行搜索,我会得到常规搜索结果,而不是Google的特殊有趣结果。
有没有一种方法可以模仿搜索以返回特殊结果,然后将数据存储在文件中或某处?
编辑:
Google似乎阻止了iframe
<html>
<div>
<iframe src="https://www.google.com/search?q=fun+facts"></iframe>
</div>
</html>
现在继续使用纯JavaScript和window.open
<script>
var win = window.open("https://www.google.com/search?q=fun+facts");
//wait for window to load before trying to access it
</script>
编辑2:
我似乎无法解决跨域问题。有没有办法打开一个窗口并从不在同一域中的窗口中提取html?我似乎无法找到一种方法来完成我想做的事情。
最佳答案
您可以使用选择器。
这捕获了一个问题:
$('[data-md=137] > div > div:nth-child(1)')
这是一个答案:
$('[data-md=137] > div > div:nth-child(2) > :nth-child(2)')
而这个捕获了源:
('[data-md=137] > div > div:nth-child(3) p:last-child')
如果要测试它们,请转到 https://www.google.com/search?q=fun+facts并拉起控制台(F12),首先在控制台中加载jQuery:
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
...然后尝试上面的选择器。
关于javascript - 从Google搜索中提取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32611810/