从 Google 搜索中提取数据
Extract Data from Google search
我正在尝试从 Google 的新有趣事实功能中提取事实。如果您在 google 中搜索有趣的事实,您会得到一个问题和一个答案。我想将这些事实存储在某个地方以备后用。
我曾尝试使用 javascript 来提取 div 有趣的事实。但是,Google 的 div 是动态的,每次都在变化你搜索。如果我尝试使用 API 或只是尝试使用 https://www.google.com/search?q=fun+facts 执行搜索,我得到的是常规搜索结果,但不是 Google 的特殊有趣事实结果。
有没有一种方法可以模拟对 return 特殊结果的搜索,然后将数据存储在文件或其他地方?
编辑:
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:
我似乎无法解决跨域问题。有没有办法打开 window 并从中提取不在同一域中的 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();
...然后尝试上面的选择器。
我正在尝试从 Google 的新有趣事实功能中提取事实。如果您在 google 中搜索有趣的事实,您会得到一个问题和一个答案。我想将这些事实存储在某个地方以备后用。
我曾尝试使用 javascript 来提取 div 有趣的事实。但是,Google 的 div 是动态的,每次都在变化你搜索。如果我尝试使用 API 或只是尝试使用 https://www.google.com/search?q=fun+facts 执行搜索,我得到的是常规搜索结果,但不是 Google 的特殊有趣事实结果。
有没有一种方法可以模拟对 return 特殊结果的搜索,然后将数据存储在文件或其他地方?
编辑: 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: 我似乎无法解决跨域问题。有没有办法打开 window 并从中提取不在同一域中的 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();
...然后尝试上面的选择器。