Подтвердить что ты не робот

Извлечение данных сайта через веб-искатель выводит ошибку из-за неправильного совпадения индекса массива

Я пытался извлечь текст таблицы сайта вместе со своей ссылкой из данной таблицы (которая находится в site1.com) на мою страницу php с помощью веб-искателя.

Но, к сожалению, из-за некорректного ввода индекса Array в php-код в него выводилась ошибка.

site1.com

<table border="0" cellpadding="0" cellspacing="0" width="100%" class="Table2">
<tbody><tr>
    <td width="1%" valign="top" class="Title2">&nbsp;</td>
    <td width="65%" valign="top" class="Title2">Subject</td>
    <td width="1%" valign="top" class="Title2">&nbsp;</td>
    <td width="14%" valign="top" align="Center" class="Title2">Last Update</td>
    <td width="1%" valign="top" class="Title2">&nbsp;</td>
    <td width="8%" valign="top" align="Center" class="Title2">Replies</td>
    <td width="1%" valign="top" class="Title2">&nbsp;</td>
    <td width="9%" valign="top" align="Center" class="Title2">Views</td>
</tr>
<tr>
    <td width="1%" height="25">&nbsp;</td>
    <td width="64%" height="25" class="FootNotes2"><a href="/files/forum/2017/1/837110.php" target="_top" class="Links2">Serious dedicated study partner for U World</a> - step12013</td>
    <td width="1%" height="25">&nbsp;</td>
    <td width="14%" height="25" class="FootNotes2" align="center">02/11/17 01:50</td>
    <td width="1%" height="25">&nbsp;</td>
    <td width="8%" height="25" align="Center" class="FootNotes2">10</td>
    <td width="1%" height="25">&nbsp;</td>
    <td width="9%" height="25" align="Center" class="FootNotes2">318</td>
</tr>
</tbody>
</table>

php. веб-искатель как:

<?php
    function get_data($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);
    return $result;
    }
    $returned_content = get_data('http://www.usmleforum.com/forum/index.php?forum=1');
    $first_step = explode( '<table class="Table2">' , $returned_content );
    $second_step = explode('</table>', $first_step[0]);
    $third_step = explode('<tr>', $second_step[1]);
    // print_r($third_step);
    foreach ($third_step as $key=>$element) {
    $child_first = explode( '<td class="FootNotes2"' , $element );
    $child_second = explode( '</td>' , $child_first[1] );
    $child_third = explode( '<a href=' , $child_second[0] );
    $child_fourth = explode( '</a>' , $child_third[0] );
    $final = "<a href=".$child_fourth[0]."</a></br>";
?>

<li target="_blank" class="itemtitle">
    <?php echo $final?>
</li>

<?php
    if($key==10){
       break;
        }
    }
?>

Теперь индексом массива на указанном выше PHP-коде может быть виновником. (Я полагаю) Если да, может кто-нибудь объяснить мне, как сделать эту работу.

Но каково мое последнее требование от этого кода: чтобы получить вышеприведенный текст во втором со ссылкой, связанной с ним.

Любая помощь приветствуется.

4b9b3361

Ответ 1

Используя Простую HTML DOM Parser, вы можете использовать следующий код:

<?php
    require('simple_html_dom.php'); // you might need to change this, depending on where you saved the library file.

    $html = file_get_html('http://www.usmleforum.com/forum/index.php?forum=1');

    foreach($html->find('td.FootNotes2 a') as $element) { // find all <a>-elements inside a <td class="FootNotes2">-element
        $element->href = "http://www.usmleforum.com" . $element->href;  // you can also access only certain attributes of the elements (e.g. the url).
        echo $element.'</br>';  // do something with the elements.
    }
?>

Ответ 2

Вместо написания собственного решения парсера вы можете использовать существующий компонент, такой как компонент Symfony DomCrawler: http://symfony.com/doc/current/components/dom_crawler.html

$crawler = new Crawler($returned_content);
$linkTexts = $crawler->filterXPath('//a')->each(function (Crawler $node, $i) {
    return $node->text();
});

Или, если вы хотите пересечь дерево DOM самостоятельно, вы можете использовать DOMDocument loadHTML http://php.net/manual/en/domdocument.loadhtml.php

$document = new DOMDocument();
$document->loadHTML($returned_content);
foreach ($document->getElementsByTagName('a') as $link) {
    $text = $link->nodeValue;
}

EDIT:

Чтобы получить нужные ссылки, код предполагает, что у вас есть переменная $returned_content с HTML, который вы хотите проанализировать.

// creating a new instance of DOMDocument (DOM = Document Object Model)
$domDocument = new DOMDocument();
// save previous libxml error reporting and set error reporting to internal
// to be able to parse not well formed HTML doc
$previousErrorReporting = libxml_use_internal_errors(true);
$domDocument->loadHTML($returned_content);
libxml_use_internal_errors($previousErrorReporting);
$links = [];
/** @var DOMElement $node */
// getting all <a> element from the HTML
foreach ($domDocument->getElementsByTagName('a') as $node) {
    $parentNode = $node->parentNode;
    // checking if the <a> is under a <td> that has class="FootNotes2"
    $isChildOfAFootNotesTd = $parentNode->nodeName === 'td' && $parentNode->getAttribute('class') === 'FootNotes2';
    // checking if the <a> has class="Links2"
    $isLinkOfLink2Class = $node->getAttribute('class') == 'Links2';
    // as I assumed you wanted links from the <td> this check makes sure that both of the above conditions are fulfilled
    if ($isChildOfAFootNotesTd && $isLinkOfLink2Class) {
        $links[] = [
            'href' => $node->getAttribute('href'),
            'text' => $parentNode->textContent,
        ];
    }
}

print_r($links);

Это создаст вам массив, похожий на:

Array
(
    [0] => Array
    (
        [href] => /files/forum/2017/1/837242.php
        [text] => [email protected] Drill Time ① - cardio69
    ) 
    [1] => Array
    (
        [href] => /files/forum/2017/1/837356.php
        [text] => study partner in Houston - lacy
    )
    [2] => Array
    (
        [href] => /files/forum/2017/1/837110.php
        [text] => Serious dedicated study partner for U World - step12013
    )
    ...

Ответ 3

Я попробовал тот же код для другого сайта. и это работает как шарм.. Пожалуйста, взгляните на это:

<?php
function get_data($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}
$returned_content = get_data('http://www.usmle-forums.com/usmle-step-1-forum/');
$first_step = explode( '<tbody id="threadbits_forum_26">' , $returned_content );
$second_step = explode('</tbody>', $first_step[1]);
$third_step = explode('<tr>', $second_step[0]);
// print_r($third_step);
foreach ($third_step as $element) {
$child_first = explode( '<td class="alt1"' , $element );
$child_second = explode( '</td>' , $child_first[1] );
$child_third = explode( '<a href=' , $child_second[0] );
$child_fourth = explode( '</a>' , $child_third[1] );
echo $final = "<a href=".$child_fourth[0]."</a></br>";
}
?>

И HTML для него:

<tbody id="threadbits_forum_26">

<tr>
<td class="alt2" colspan="2"><img src="/images/icons/promo_red.png" alt="Advertisement" border="0" title="Advertisement">
</td>
<td class="alt1" id="td_threadtitle_50499" title="">    
<div>   
<span style="float:right"><img class="inlineimg" src="/images/misc/tag.png" alt="Ads-Services, USMLE-Success-Academy-" title="Ads-Services, USMLE-Success-Academy-"><img class="inlineimg" src="/images/misc/sticky.gif" alt="Sticky Thread" title="Sticky Thread"> </span>     
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/50499-pass-your-step-1-exam-100-guaranteed.html'; return false;" id="thread_gotonew_50499"><img class="inlineimg" src="/images/buttons/firstnew.gif" alt="Go to first new post" border="0" title="Go to first new post"></a>
            Sticky:         
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/50499-pass-your-step-1-exam-100-guaranteed.html'; return false;" id="thread_title_50499" style="font-weight:bold">Pass Your Step 1 Exam - 100% Guaranteed!</a>      
</div>
<div class="smallfont"> 
<span style="cursor:pointer" onclick="window.open('http://www.usmle-forums.com/members/usmle-success-academy.html', '_self')">USMLE-Success-Academy</span>      
</div>
</td>
<td class="alt2" title="Replies: 51, Views: 32,107">
<div class="smallfont" style="text-align:right; white-space:nowrap">
            3 Days Ago <br>by 
<a href="#" onclick="location.href='http://www.usmle-forums.com/members/hematocrit.html'; return false;">hematocrit</a>  <a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/50499-pass-your-step-1-exam-100 guaranteed.html#post1400458'; return false;"><img class="inlineimg" src="/images/buttons/lastpost.gif" alt="Go to last post" border="0" title="Go to last post"></a>
</div>
</td>   
<td class="alt1" align="center"><a rel="nofollow" href="#" onclick="location.href='http://www.usmle-forums.com/misc.php?do=whoposted&amp;t=50499'; return false;" onclick="who(50499); return false;">51</a>
</td>
<td class="alt2" align="center">32,107
</td>   
</tr>

<tr>
<td class="alt2" colspan="2"><img src="/images/icons/favorite.png" alt="Star" border="0" title="Star">
</td>
<td class="alt1" id="td_threadtitle_247650" title="">   
<div>
<span style="float:right"><img class="inlineimg" src="/images/misc/sticky.gif" alt="Sticky Thread" title="Sticky Thread"></span>            
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/247650-usmle-step-1-live-prep-boot-camp-chicago.html'; return false;" id="thread_gotonew_247650"><img class="inlineimg" src="/images/buttons/firstnew.gif" alt="Go to first new post" border="0" title="Go to first new post"></a>          
            Sticky:             
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/247650-usmle-step-1-live-prep-boot-camp-chicago.html'; return false;" id="thread_title_247650" style="font-weight:bold">USMLE Step 1 Live prep/Boot Camp in Chicago</a>         
</div>
<div class="smallfont">
<span style="cursor:pointer" onclick="window.open('http://www.usmle-forums.com/members/medical-legends.html', '_self')">Medical-Legends</span>      
</div>
</td>   
<td class="alt2" title="Replies: 11, Views: 4,248">
<div class="smallfont" style="text-align:right; white-space:nowrap">
            1 Week Ago <br>by 
<a href="#" onclick="location.href='http://www.usmle-forums.com/members/svbobbili.html'; return false;">svbobbili</a>  
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/247650-usmle-step-1-live-prep-boot-camp-chicago.html#post1396290'; return false;"><img class="inlineimg" src="/images/buttons/lastpost.gif" alt="Go to last post" border="0" title="Go to last post"></a>
</div>
</td>
<td class="alt1" align="center">
<a rel="nofollow" href="#" onclick="location.href='http://www.usmle-forums.com/misc.php?do=whoposted&amp;t=247650'; return false;" onclick="who(247650); return false;">11</a>
</td>
<td class="alt2" align="center">4,248
</td>   
</tr>

<tr>
<td class="alt2" colspan="2"><img src="/images/icons/promo_red.png" alt="Advertisement" border="0" title="Advertisement">
</td>
<td class="alt1" id="td_threadtitle_47902" title="">    
<div>       
<span style="float:right">
<img class="inlineimg" src="/images/misc/tag.png" alt="Ads-Services, USMLEstat-" title="Ads-Services, USMLEstat-">              
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/#'; return false;" onclick="attachments(47902); return false"> <img class="inlineimg" src="/images/misc/paperclip.gif" border="0" alt="1 Attachment(s)" title="1 Attachment(s)"></a>            
<img class="inlineimg" src="/images/misc/sticky.gif" alt="Sticky Thread" title="Sticky Thread"></span>
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/47902-new-completely-free-usmle-step-1-resource-usmlestat.html'; return false;" id="thread_gotonew_47902"><img class="inlineimg" src="/images/buttons/firstnew.gif" alt="Go to first new post" border="0" title="Go to first new post"></a>     
            Sticky:         
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/47902-new-completely-free-usmle-step-1-resource-usmlestat.html'; return false;" id="thread_title_47902" style="font-weight:bold">New, Completely free USMLE Step 1 resource - USMLEstat</a>     
</div>
<div class="smallfont">     
  <span style="cursor:pointer" onclick="window.open('http://www.usmle forums.com/members/usmlestat.html', '_self')">USMLEstat</span>            
</div>
</td>
<td class="alt2" title="Replies: 98, Views: 1,120,979">
<div class="smallfont" style="text-align:right; white-space:nowrap">
            1 Week Ago <br>by 
<a href="#" onclick="location.href='http://www.usmle-forums.com/members/svbobbili.html'; return false;">svbobbili</a>  
<a href="#" onclick="location.href='http://www.usmle-forums.com/usmle-step-1-forum/47902-new-completely-free-usmle-step-1-resource-usmlestat.html#post1396274'; return false;"><img class="inlineimg" src="/images/buttons/lastpost.gif" alt="Go to last post" border="0" title="Go to last post"></a>
</div>
   </td>    
   <td class="alt1" align="center"><a rel="nofollow" href="#" onclick="location.href='http://www.usmle-forums.com/misc.php?do=whoposted&amp;t=47902'; return false;" onclick="who(47902); return false;">98</a></td>
    <td class="alt2" align="center">1,120,979</td>  
</tr>
</tbody>

Я знаю его слишком много, чтобы спросить, но можете ли вы сделать код из этих двух, которые заставляют искателя работать.

@jkmak