Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回相同结果的倍数_Php - Fatal编程技术网

Php 返回相同结果的倍数

Php 返回相同结果的倍数,php,Php,我目前正在使用simple_html_dom来抓取一个网站,所有内容都被抓取了,但它多次显示了抓取的数据 这是我正在尝试刮的选择器,我认为这是导致问题的原因 #wrapper > div.container > div > h3 > a 您可以查看我的示例[链接已删除] 这里的代码是我目前用来刮取的 $html = file_get_html('http://www.example.com/sitefile.php?s=1&page='.$page.'');

我目前正在使用simple_html_dom来抓取一个网站,所有内容都被抓取了,但它多次显示了抓取的数据

这是我正在尝试刮的选择器,我认为这是导致问题的原因

#wrapper > div.container > div > h3 > a
您可以查看我的示例[链接已删除]

这里的代码是我目前用来刮取的

$html = file_get_html('http://www.example.com/sitefile.php?s=1&page='.$page.'');

foreach($html->find('#wrapper > div.container > div: > h3') as $element) 
{
    echo '<br><br>';
    echo $url = $element->href;

    $html2 = file_get_html($url);

    echo '<br>';    

    $title = $html2->find('#primary > div > div > div > h1',0);
    echo $title = ucwords(strtolower($title->plaintext));

    echo '<br>';

    $youtube = $html2->find('#Playerholder > iframe',0);

    preg_match("/embed\/(.*)\?/", $youtube->src, $output_array);

    echo $youtube = $output_array[1];       
}
$html=file\u get\u html('http://www.example.com/sitefile.php?s=1&page=“.$page.”);
foreach($html->find('#wrapper>div.container>div:>h3')作为$element)
{
回音“

”; echo$url=$element->href; $html2=文件\获取\ html($url); 回声“
”; $title=$html2->find('#primary>div>div>div>h1',0); echo$title=ucwords(strtolower($title->明文)); 回声“
”; $youtube=$html2->find('Playerholder>iframe',0); preg\u match(“/embed\/(.*)\?/”,$youtube->src,$output\u数组); echo$youtube=$output_数组[1]; }
找到了问题

显然,您试图从中获取内容的网站有两个不同的位置,使用相同的选择器,您正在为
#wrapper>div.container>div>h3>a
,这就是它返回多个结果的原因,所以请尝试将此
foreach($html->find('#wrapper>div.container>div:>h3')更改为$element)
到这个
foreach($html->find('.omega grid>h3>a')作为$element)
你应该有你想要的输出

编辑:

foreach($html->find('.omega-grid > h3 > a') as $element) 
{
    echo '<br><br>';
    echo $url = $element->href;

    $html2 = file_get_html($url);

    echo '<br>';    

    $title = $html2->find('#primary > div > div > div > h1',0);
    echo $title = ucwords(strtolower($title->plaintext));

    echo '<br>';

    $youtube = $html2->find('#Playerholder > iframe',0);

    preg_match("/embed\/(.*)\?/", $youtube->src, $output_array);

    echo $youtube = $output_array[1];       
}
foreach($html->find('.omega grid>h3>a')作为$element)
{
回音“

”; echo$url=$element->href; $html2=文件\获取\ html($url); 回声“
”; $title=$html2->find('#primary>div>div>div>h1',0); echo$title=ucwords(strtolower($title->明文)); 回声“
”; $youtube=$html2->find('Playerholder>iframe',0); preg\u match(“/embed\/(.*)\?/”,$youtube->src,$output\u数组); echo$youtube=$output_数组[1]; }
可能是因为您使用了
#wrapper>div.container>div:>h3
而不是
#wrapper>div.container>div>h3