【问题标题】:Code to find strings in source code over many urls-timeout issue通过许多 urls-timeout 问题在源代码中查找字符串的代码
【发布时间】:2011-04-13 04:08:21
【问题描述】:

我想输入一个很长的 url 列表并在源代码中搜索特定的字符串,输出包含该字符串的 url 列表。听起来很简单吧?我有下面的代码,你可以在 pelican-cement.com/findfrog 试试。

问题是每次我搜索超过 10 个左右的 url 时都会超时

<html>
<body>

<form action="search.php" method="post"> 
  URLs: <br/>
  <textarea rows="20" cols="50" input type="text" name="url" /></textarea><br/>

  Search Term: <br/>
  <textarea rows="20" cols="50" input type="text" name="proxy" /></textarea><br/>

  <input type="submit" /> 
</form>

<?
set_time_limit (0);
  if(isset($_POST['url'])) {


    $urls = explode("\n", $_POST['url']);
    $term = $_POST['proxy'];
    $options = array( CURLOPT_FOLLOWLOCATION => 1,
                      CURLOPT_RETURNTRANSFER => 1,
                      CURLOPT_CUSTOMREQUEST  => 'GET',
                      CURLOPT_HEADER         => 1,
                      );
    $ch = curl_init();
    curl_setopt_array($ch, $options);

    foreach ($urls as $url) {
      curl_setopt($ch, CURLOPT_URL, trim($url));
      $html = curl_exec($ch);

      if ($html !== FALSE && stristr($html, $term) !== FALSE) { // Found!
        echo $url;
        echo "<br>";
      }
    }

    curl_close($ch);
    echo "space";
  }
?>
</html>

【问题讨论】:

  • 是的。这就是来龙去脉。许多请求会占用时间。没有神奇的解决方案。
  • 这是相同的代码,但这是一个不同的问题。我可以等,问题是它超时太早了。问题不是花费时间,而是没有返回结果。

标签: php arrays curl timeout web-scraping


【解决方案1】:

尝试修改时间限制。

foreach ($urls as $url) {
  set_time_limit(120);
  curl_setopt($ch, CURLOPT_URL, trim($url));
  $html = curl_exec($ch);

每个 url 2 分钟

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2017-08-14
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多