简体   繁体   English

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

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

I have tried 我努力了

<?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 HTML

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

Codes are on the same page. 代码在同一页面上。 I just typed them like this to be more clear. 我只是这样输入它们就更清楚了。

Main problem is that by default it tells me on the screen indexed . 主要问题是默认情况下它会在屏幕indexed上告诉我。 If i type any url it will say as well indexed . 如果我输入任何网址,它会说indexed For example I type the url in the textarea jhbsadhbahsd545.com, it returns indexed when it should have returned not indexed . 例如,我在textarea jhbsadhbahsd545.com中键入url,它返回indexed时应返回not indexed What have I done wrong? 我做错了什么?

strpos can return 0 which is a falsy value. strpos可以返回0,这是一个假值。 Compare with === 与===比较

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

However I believe this won't work as Google does not return the string with the first response that you are looking for, but rather lazy loading once the page has been loaded with javascript. 但是我相信这不会起作用,因为Google没有返回您正在寻找的第一个响应的字符串,而是在页面加载了javascript后延迟加载。

Open up Chrome and view-source:https://www.google.com/#q=site:hopefullythisisadomainthatdoesnotexists.com to check what does Google return and why is it always missing. 打开Chrome和view-source:https://www.google.com/#q=site:hopefullythisisadomainthatdoesnotexists.com查看Google返回的内容以及为什么它总是丢失。


Also change the URL you are making the request to from: 同时更改您发出请求的URL:

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

to: 至:

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

So you cannot scrape content from Google that way, they actually prohibit you from doing it. 所以你不能以这种方式从谷歌中删除内容,他们实际上禁止你这样做。 You'll need to utilize their API to do what you're needing. 您需要利用他们的API来完成您的需求。

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

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

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