【问题标题】:My Site has slowed down because of usage of Simple_html_dom in php [closed]由于在 php 中使用 Simple_html_dom,我的网站变慢了 [关闭]
【发布时间】:2012-12-23 03:46:38
【问题描述】:

我将 simple_html_dom 用于我的社交书签网站,例如 reddit,其中每页显示 15 个链接。每次我加载页面,都需要很长时间。例如,我使用类似下面的代码从新闻网站中提取数据。

$html = file_get_html('http://www.nytimes.com');
$img= $html->find('img',6);
echo'<img src="'.$img->src.'"style="height:100px;width:100px;float:left;margin-right:5px"/>';
$title = array_shift($html->find('title'))->innertext;
echo '<p style="font-size:13px"><strong>'.$title.'</strong></p>';
foreach($html->find('div') as $element)
 if($element->class=='article_txt'){
   echo $element->find('p',0);
   } 

是不是因为我使用了这段代码,导致我的网站需要花费太多时间来提取数据和显示?如果是,那么如何减少数据提取和显示的时间?

【问题讨论】:

  • @Dagon 你能说得更具体些吗
  • developer.nytimes.com/docs 如果它不能通过 api 的 onre 获得,那么你只是在偷窃
  • 仅仅因为某些东西在别人的网站上并没有给你权利在你的网站上使用它,或者以任何其他方式
  • 得到它..会照顾它..

标签: php screen-scraping simple-html-dom


【解决方案1】:

每个新请求都会导致 file_get_html 函数获取远程数据,显然,您必须等待它完成。您应该使用 memcached http://php.net/manual/en/book.memcache.php 之类的东西缓存这些结果。设置好 memcached 和 Memcache 后,你可以这样做:

// You'd have to set it up before usage
$cache = new Memcache();
$key = md5('the-url-goes-here');

if (!($html = $cache->get($key)))
{
    $html = file_get_html('http://www.nytimes.com');
    $cache->set($key, $html);
}

// other code that uses $html

【讨论】:

  • 我完全没听懂你的意思
  • 我在我的网站上共享主机,所以我不能使用 memcache.. 你能建议我其他方式吗?
  • 如果您无法说服他们启用 memcache,您可以使用文本文件滚动您自己的缓存系统。您可以将每个请求保存在文本文件中,也可以缓存整个页面,直到添加新链接。您只需将 HTML 输出保存在一个文件中即可。检查这个:gist.github.com/4363114
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 2011-01-14
相关资源
最近更新 更多