【发布时间】:2019-06-12 07:21:31
【问题描述】:
我对 php 非常陌生,并且尝试了一些方法来尝试抓取 2 个站点中的 1 个。这些站点中的任何一个都可以用于我正在寻找的数据。 试图弄清楚如何使用 youtube 视频和其他资源来做到这一点。 可能这对我来说太复杂了。使用 CURL、DOM 等,但正在努力让我绕过它。 我只想返回日期和时间、比赛路线和赛马名称。
我想知道是否有人可以帮助解释在哪里查看或使用什么代码。甚至我哪里出错了。 我将以表格格式将此代码集成到 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);
?>
【问题讨论】:
标签: php curl dom web-scraping