Php 刮取第二个HTML表

Php 刮取第二个HTML表,php,json,web-scraping,Php,Json,Web Scraping,我试图从网站中提取主表,将其转换为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

我试图从网站中提取主表,将其转换为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;
}?>
而不是

{
"chartDate" : "", 
"retrieved" : "1444101246", 
"entries" : 
[
    {
        "position" : "2", 
        "plus" : "1", 
        "artist" : "KENNY CHESNEY", 
        "noWeeks" : "Save It For A Rainy"", etc . etc.
    }, 
]
}
我可以向上面的代码添加什么来检索该表?

更新 问题在于匹配模式。 在以下声明之后

$content = str_replace($newlines, "", html_entity_decode($content));
一些字符被替换或删除,例如
,一些标记是大写的。因此,无论
$start\search
包含什么内容,您都会得到
0
作为
$scrape\u start
的strpos

所以你必须像这样搜索

$start_search ='<TBODY>';
$start\u search='';

上运行代码感谢您的回复,呃。我之前确实尝试过,但没有任何更改:(您尝试上面的内容时得到了什么?相同的输出?是的,相同的输出。嘿,非常感谢!它看起来正常工作。现在的问题是返回的数组有蓝色行(LW,TW,Artister).你能告诉我我能不能摆脱它们吗?如果不能,那没关系。你已经做得够多了。是的,我会尝试去除它们。等等。@PaulCrovella嘿,谢谢。我是php新手,我希望我能理解所有这些,但我想看看。
$start_search ='<TBODY>';