如何使用php在源代码中搜索字符串


How to search in the source code for a string with php?

我试过

<?php
  $url = $_POST['attributename'];
  $needtofind = "did not match any documents.  </p>";
  $site = file_get_contents("https://www.google.com/#q=site:$url");
  if(strpos($site, $needtofind) == false) {
    echo 'indexed';
  } else {
    echo 'not indexed';
  } 
  ob_end_clean();
?>

HTML

<div class="center-page">
  <form method="POST">
    <textarea id="float" name="attributename" value=""></textarea><br/>
    <input type="submit" value="Go" />
  </form>
</div>

代码在同一页上。我只是把它们打成这样更清楚。

主要问题是默认情况下它在屏幕indexed上告诉我。如果我键入任何url,它也会说indexed。例如,我在文本区域jhbsadhbahsd545.com中键入url,它在本应返回not indexed时返回indexed。我做错了什么?

strpos可以返回0,这是一个错误值。与===比较

strpos($site, $needtofind) === false

然而,我相信这不会起作用,因为谷歌不会返回你想要的第一个响应的字符串,而是在页面加载了javascript后延迟加载。

打开Chrome和view-source:https://www.google.com/#q=site:hopefullythisisadomainthatdoesnotexists.com,查看谷歌返回了什么以及为什么总是缺少它。


同时将您请求的URL从以下位置更改为:

https://www.google.com/#q=site:$url

至:

https://www.google.com/search?q=site:$url

所以你不能用这种方式从谷歌抓取内容,他们实际上禁止你这样做。你需要利用他们的API来做你需要的事情。

https://developers.google.com/custom-search/json-api/v1/overview