나는 간단한 HTML DOM 파서를 사용하고 있습니다 - http://simplehtmldom.sourceforge.net/manual.htm 나는 점수 판 페이지에서 일부 데이터를 긁어 노력하고있어. 아래 예제는 " Akron Rushing "테이블 의 HTML을 가져 오는 것을 보여줍니다 .
내부 $tr->find('td', 0)
첫 번째 열에는 하이퍼 링크가 있습니다. 이 하이퍼 링크를 어떻게 추출 할 수 있습니까? 사용 $tr->find('td', 0')->find('a')
이 작동하지 않는 것 같습니다.
또한 : 각 테이블에 대한 조건 (통과, 돌진, 수신 등)을 작성할 수 있지만이를 수행하는 더 효율적인 방법이 있습니까? 나는 이것에 대한 아이디어에 열려 있습니다.
include('simple_html_dom.php');
$html = file_get_html('http://espn.go.com/ncf/boxscore?gameId=322432006');
$teamA['rushing'] = $html->find('table.mod-data',5);
foreach ($teamA as $type=>$data) {
switch ($type) {
# Rushing Table
case "rushing":
foreach ($data->find('tr') as $tr) {
echo $tr->find('td', 0); // First TD column (Player Name)
echo $tr->find('td', 1); // Second TD Column (Carries)
echo $tr->find('td', 2); // Third TD Column (Yards)
echo $tr->find('td', 3); // Fourth TD Column (AVG)
echo $tr->find('td', 4); // Fifth TD Column (TDs)
echo $tr->find('td', 5); // Sixth TD Column (LGs)
echo "<hr />";
}
}
}
귀하의 경우에는 find('tr')
예상되는 7 행 대신 10 개의 요소를 반환합니다.
또한 모든 이름에 연결된 링크가있는 것은 아니므로 링크가없는 경우 검색하려고하면 오류가 반환 될 수 있습니다.
따라서 다음은 코드의 수정 된 작업 버전입니다.
$url = 'http://espn.go.com/ncf/boxscore?gameId=322432006';
$html = file_get_html('http://espn.go.com/ncf/boxscore?gameId=322432006');
$teamA['rushing'] = $html->find('table.mod-data',5);
foreach ($teamA as $type=>$data) {
switch ($type) {
# Rushing Table
case "rushing":
echo count($data->find('tr')) . " \$tr found !<br />";
foreach ($data->find('tr') as $key => $tr) {
$td = $tr->find('td');
if (isset($td[0])) {
echo "<br />";
echo $td[0]->plaintext . " | "; // First TD column (Player Name)
// If anchor exists
if($anchor = $td[0]->find('a', 0))
echo $anchor->href; // href
echo " | ";
echo $td[1]->plaintext . " | "; // Second TD Column (Carries)
echo $td[2]->plaintext . " | "; // Third TD Column (Yards)
echo $td[3]->plaintext . " | "; // Fourth TD Column (AVG)
echo $td[4]->plaintext . " | "; // Fifth TD Column (TDs)
echo $td[5]->plaintext; // Sixth TD Column (LGs)
echo "<hr />";
}
}
}
}
보시다시피이 형식을 사용하여 속성을 변경할 수 있습니다 $tag->attributeName
. 귀하의 경우 attributeName
에는href
아무것도 발견되지 않으면 "False"를 반환한다는 것을 알고 find 오류를 처리하는 것이 좋습니다.
$td = $tr->find('td');
// Find suceeded
if ($td) {
// code here
}
else
echo "Find() failed in XXXXX";
PHP Simple HTML DOM Parser에는 php5에서 알려진 메모리 누수 문제가 있으므로 DOM 객체가 더 이상 사용되지 않을 때 메모리를 확보하는 것을 잊지 마십시오.
$html = file_get_html(...);
// do something...
$html->clear();
unset($html);
Source: http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다