【问题标题】:Newbie trying to scrape site with no luck新手试图在没有运气的情况下抓取网站
【发布时间】: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


    【解决方案1】:

    尝试使用另一种方法替换 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);
    

    :) 祝你好运

    【讨论】:

    • 这完全让我大吃一惊:-(我不知道从哪里开始
    • @dave 检查我的评论
    • 谢谢,但是当我尝试运行代码时出现 500 服务器错误,抱歉,我说我真的不知道 :-(
    • 非常感谢您一直以来的支持,但是很抱歉,代码仍然无法正常工作 - 服务器错误 500
    • 我已经在我的电脑上测试了该代码,它可以工作。你的错误是什么?
    猜你喜欢
    • 2021-08-17
    • 2017-11-25
    • 2020-04-29
    • 2019-10-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    相关资源
    最近更新 更多