繁体   English   中英

使用简单HTML DOM解析器的strpos()错误

[英]strpos() error using Simple HTML DOM Parser

简单HTML DOM解析器(1分钟,39秒)

<?php
    include('simple_html_dom.php');

    $i = 0;
    $times_to_run = 100;
    set_time_limit(0);

    while ($i++ < $times_to_run) {
        // Find target image
        $url = "http://store.steampowered.com/app/".$i;
        $html = file_get_html($url);
        $element = $html->find('img.game_header_image_full');

        if($i == $times_to_run) {
            echo "Success!";
        }

        foreach($element as $key => $value){
        // Check if image was found
            if (strpos($value,'img') == false) {
                // Do nothing, repeat loop with $i++;

            } else {
                // Add (don't overwrite) to file steam.txt
                file_put_contents('steam.txt', $url.PHP_EOL , FILE_APPEND);
            }
        }
    }
?>

vs. cURL替代。.(34s)

<?php

    $i = 0;
    $times_to_run = 100;
    set_time_limit(0);

    while ($i++ < $times_to_run) {

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, 'http://store.steampowered.com/app/'.$i);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
        $content = curl_exec($ch);

        $url = "http://store.steampowered.com/app/".$i;

        $reg = "/<\\s*img\\s+[^>]*class=['\"][^'\"]*game_header_image_full[^'\"]*['\"]/i";

        if(preg_match($reg, $content)) {
            file_put_contents('steam.txt', $url.PHP_EOL , FILE_APPEND);
        }

    }

?>

由于某种原因,第一个成功捕获了9个URL,而第二个(cURL)仅捕获了带有preg_match的8个URL。 它会自动超时还是会超时,或者preg_match是否不如$html->find('img.game_header_image_full')

问题N 1

尝试在数组上循环,然后在循环内使用strpos ..(适应于您需要strpos评估

<?php
include('simple_html_dom.php');

$i = 0;
$times_to_run = 100;

while ($i++ < $times_to_run) {
    // Find target image
    $url = "http://store.steampowered.com/app/".$i;
    $html = file_get_html($url);
    $element = $html->find('img.game_header_image_full');

    forarch($elemnt as $key => $value){
         // Check if image was found
         if (strpos($value,'img') == false) {
             // Do nothing, repeat loop with $i++;

         } else {
            // Add (don't overwrite) to file steam.txt
        file_put_contents('steam.txt', $url.PHP_EOL , FILE_APPEND);
       }
    }
}
?>

有理由认为,您只会对$element不为空的情况感兴趣:

$found = $html->find('img.game_header_image_full');
if(!empty($found)) {
    file_put_contents('steam.txt', $url.PHP_EOL , FILE_APPEND);
}

这样也可以避免$html->find()返回多个结果的情况(考虑到选择器是基于类的,而不是基于ID的,则可能会发生这种情况)。

此外,您可能会考虑使用HTML解析器对性能的影响。 使用Curl检索数据和使用正则表达式查找元素的性能要好得多(因为您只在查找类名,而HTML采用可预测的格式):

$reg = "/<\\s*img\\s+[^>]*class=['\"][^'\"]*game_header_image_full[^'\"]*['\"]/i";
if(preg_match($reg, $raw_source)) {
    file_put_contents('steam.txt', $url.PHP_EOL , FILE_APPEND);
}

可能会表现得更好。 正则表达式本质上说:

Starts with an open tag.
Zero or more whitespace.
Tag is `img`
Zero or more whitespace.
Any character that is not an end-tag.
Class declaration.
The class declaration contains `game_header_image_full`.

在这里工作: https//regex101.com/r/fQ8lJ1/1

此外:如果您使用Multi Curl( 如此处所述) ,您将看到无法想象的速度。

最后一件事:如果您是从命令行执行此操作,请考虑使用简单的echo语句,而不是追加。 这将允许您进行测试,并且可以在运行时将其追加到文件中,如下所示:

$ php file_to_execute.php >> steam.txt

暂无
暂无

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

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