简体   繁体   English

如何在Google结果页面中提取搜索结果总数

[英]How to extract total amount of search results in Google results page

I am trying to find and extract the "About 16,600,000 results (0.69 seconds)" text from the Google results page. 我正在尝试从Google结果页中查找并提取“大约16,600,000个结果(0.69秒)”文本。 Not the results, just the String text. 不是结果,只有String文本。 After extracting, i want to save the result in an Array and to repeat it twice with other keywords. 提取后,我想将结果保存在数组中,并与其他关键字重复两次。 Afterwards, i will loop through the Array to print it in a table. 之后,我将遍历数组以将其打印在表中。 I don't know why, but if i try to find "results" on the page i always tells me it did not find anything. 我不知道为什么,但是如果我尝试在页面上找到“结果”,我总是告诉我它什么也没找到。 Current code: 当前代码:

<?php
$url = "https://www.google.co.uk/#q=cheese";
$html = file_get_contents($url);
if (strpos($html, "results") !== false) {
  echo "found";
} else {
  echo "not found";
}
?>

I think it's because https://www.google.co.uk/#q=cheese only returns a html document, which runs some JavaScript code that fetches the search result in a second call. 我认为这是因为https://www.google.co.uk/#q=cheese仅返回html文档,该文档运行一些JavaScript代码,可在第二次调用中获取搜索结果。

(You can disable JavaScript in your browser and visit the page to "see" what file_get_contents would see) (您可以在浏览器中禁用JavaScript并访问该页面以“查看” file_get_contents将看到的内容)

You won't be able to do this using file_get_contents or curl as the other answer already pointed out. 正如其他答案已经指出的那样,您将无法使用file_get_contentscurl来执行此操作。

You probably want to look at something like PhantomJS for this. 您可能想要为此查看类似PhantomJS的内容。 PhantomJS is a WebKit browser and will enable you to get the results you are lookng for. PhantomJS是一个WebKit浏览器,它将使您能够获得想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM