【问题标题】:PHP web based scraper基于 PHP 网页的爬虫
【发布时间】:2012-11-24 06:24:42
【问题描述】:

我想做的是使用 PHP 来抓取我输入参数的网址的网站。

我想要完整的原始源代码..但这还不是全部..

我希望它然后保存到一个 html 页面中,并保存到 php 脚本的本地服务器上。

有一个简单的片段吗?或者有人可以轻松地为我编写代码吗?

例如

我要刮http://google.com

例如,mysite.com/scrape.php?url=http://google.com

我想让它把google的首页保存到http://mysite.com/scraped/google.com.html

【问题讨论】:

  • scrape 你的意思是download
  • 看看file_get_contents()file_put_contents() 的文档怎么样

标签: php html scrape


【解决方案1】:

这是一个脚本,它将指定 url 的内容保存到一个名为 scraped.html 的文件中:

if (isset($_GET['url'])):
   $contents = file_get_contents($_GET['url']);
   file_put_contents('scraped.html', $contents);
endif;

要在对file_get_contents() 的调用中使用网址,您必须在php.ini 文件中启用allow_url_fopen

当然,这只会保存请求的 url 的实际来源,而不是任何其他资源,例如图像、脚本和样式表。

【讨论】:

    最近更新 更多