【问题标题】:Scrape second HTML table抓取第二个 HTML 表格
【发布时间】:2015-10-06 03:32:38
【问题描述】:

我正在尝试从网站中提取主表,将其转换为 JSON,但我想要的表之前的表阻碍了我正在使用的代码。我正在使用的代码:

<?php 


$singles_chart_url = 'http://www.mediabase.com/mmrweb/allaboutcountry/Charts.asp?format=C1R';

// Get the mode from the user:
$mode = $_GET['chart'];

// This is an array of elements to remove from the content before stripping it:
$newlines = array("\t", "\n", "\r", "\x20\x20", "\0", "\x0B");

switch($mode)
{
    // They want the Singles chart, or haven't specified what they want:
    case 'singles':
    case '':
    default:
        $content = file_get_contents($singles_chart_url);
        $start_search = '<table width="100%" border="0" cellpadding="2" cellspacing="2">';
        break;
    
    
}

$content = str_replace($newlines, "", html_entity_decode($content));
$scrape_start = strpos($content, $start_search);


$scrape_end   = strpos($content, '</table>', $scrape_start);
$the_table    = substr($content, $scrape_start, ($scrape_end - $scrape_start));



// Now loop through the rows and get the data we need:
preg_match_all("|<tr(.*)</tr>|U", $the_table, $rows);

// Set the heading so we can output nice XML:
switch($_REQUEST['format'])
{

    
    case 'json':
    default:
        header('Content-type: application/json');

        
        $count = 0;
        foreach($rows[0] as $row)
        {
            // Check it's OK:
            if(!strpos($row, '<th'))
            {
                // Get the cells:
                preg_match_all("|<td(.*)</td>|U", $row, $cells);
                $cells = $cells[0];
                
                $position = strip_tags($cells[0]);
                $plus = strip_tags($cells[1]);
                $artist   = strip_tags($cells[2]);
                $weeks    = strip_tags($cells[3]);

                echo "\n\t\t" . '{';
                echo "\n\t\t\t" . '"position" : "' . $position . '", ';
                echo "\n\t\t\t" . '"plus" : "' . $plus . '", ';
                echo "\n\t\t\t" . '"artist" : "' . $artist . '", ';
                echo "\n\t\t\t" . '"noWeeks" : "' . $weeks . '" ';
              
    echo ($count != (count($rows[0]) - 2)) ? "\n\t\t" . '}, ' : "\n\t\t" . '}';
                $count++;
            }
        }
        echo "\n\t" . ']';
        echo "\n" . '}';
        break;
}?>

website 我正在尝试抓取。目标是检索LW、TW、Artist、Title等之后开始的表的json结果。以上返回:

{
"chartDate" : "", 
"retrieved" : "1444101246", 
"entries" : 
[
    {
        "position" : "7 DayCharts", 
        "plus" : "Country    Past 7 Days -by Overall Rank    Return to Main Menu   ", 
        "artist" : "  ", 
        "noWeeks" : "", 
        "peak" : "", 
        "points" : "", 
        "increase" : "",
        "us" : "" 
    }, 
]
}

而不是

{
"chartDate" : "", 
"retrieved" : "1444101246", 
"entries" : 
[
    {
        "position" : "2", 
        "plus" : "1", 
        "artist" : "KENNY CHESNEY", 
        "noWeeks" : "Save It For A Rainy"", etc . etc.
    }, 
]
}

我可以在上面的代码中添加什么来检索该表?

【问题讨论】:

  • @PaulCrovella 嘿,谢谢。我是 php 新手,我希望我能理解所有这些,但我会看看。

标签: php json web-scraping


【解决方案1】:

更新 问题是匹配模式。 在下面的语句之后,

$content = str_replace($newlines, "", html_entity_decode($content));

一些字符被替换或删除,例如" 和一些标记为大写。因此,无论$start_search 包含什么内容,您总是将0 作为strpos 获得$scrape_start

所以你必须像这样搜索,

$start_search ='<TBODY>';

PhpFiddle 上的工作代码

【讨论】:

  • 感谢您的回复,错误。我之前确实尝试过,但没有任何改变:(
  • 你在上面尝试时得到了什么?同样的输出?
  • 是的,同样的输出。
  • 嘿,非常感谢!它看起来像它的工作。现在的问题是返回的数组具有蓝色行(LW、TW、Artist)。你能告诉我我能不能摆脱它们吗?如果没有,那也没关系。你做的已经够多了。
  • 是的,我会尝试删除这些。等一下。
猜你喜欢
  • 2013-06-30
  • 2021-07-08
  • 2020-09-28
  • 1970-01-01
  • 2016-01-31
  • 2015-12-12
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多