新手试图刮站点没有运气

时间:2019-06-12 07:21:31

标签: php curl dom web-scraping

我对php非常陌生,并尝试了一些方法来尝试抓取2个站点中的1个。这些站点中的任何一个都可以容纳我要查找的数据。 试图弄清楚如何使用youtube视频和其他资源来做到这一点。 对我来说,这可能太复杂了。使用CURL,DOM等,但正在努力使我摆脱困境。 我只想返回DATE&Time,比赛路线和赛马名称。

我想知道是否有人可以帮助解释外观或使用什么代码。甚至我要去哪里错了。 我将以表格格式将此代码集成到wordpress页面中

<?php

include('simple_html_dom.php');
header('Content-Type: application/json');
$html=file_get_html("https://www.sportinglife.com/racing/profiles/trainer/216");
//$html=file_get_html("https://www.racingpost.com/profile/trainer/28787/richard-hannon/entries");

$row_count=0;
$json = array();

// Find all links 
$table = $html->find('table', 0);
foreach($table->find('tr') as $row) {
$day = $row->find('td',0)->plaintext;
$horse = $row->find('td',1)->innertext;
$racecourse = $row->find('td',2)->innertext;

$json[] = [ 'Date & Time' => strip_tags($day), 'Horse' => strip_tags($horse),'Racecourse' => strip_tags($racecourse)];
}

$options = array(
'http' => array(
'method'  => 'POST',
'content' => json_encode(array('Closings' =>$json)),
'header'=>  "Content-Type: application/json\r\n" .
            "Accept: application/json\r\n"
)
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

echo json_encode(array('Closings' =>$json), JSON_PRETTY_PRINT);  


?>  

1 个答案:

答案 0 :(得分:0)

尝试使用另一种方法替换file_get_html。 我建议您使用以下软件包:https://github.com/php-curl-class/php-curl-class

更多详细信息:

$url = 'https://www.sportinglife.com/racing/profiles/trainer/216';
$curl = new Curl();
$curl->get($url);
$data = $curl->response;
$curl->close();
$html = str_get_html($data);
foreach($html->find('a[class="person-profile-racing-form-racecard-link"]') as $e){      
    $date = $e->innertext;  
    echo $date.'</br>';     
};
$html->clear();
unset($html);

:)祝你好运