我正在尝试从网址保存歌曲标题:https://onlineradiobox.com/us/977todayshits/playlist
我使用以下代码获取数据
$html = file_get_contents("https://onlineradiobox.com/us/977todayshits/playlist");
$matches = array();
$output = preg_match_all('/<table class="tablelist-schedule" role="log">(.*?)</table>/s', $html, $matches,PREG_SET_ORDER );
echo "<pre>";
print_r($matches);
echo "</pre>";
上述代码的结果:
Live Mark Ronson - Nothing Breaks Like a Heart (feat. Miley Cyrus)
10:41 Camila Cabello - Consequences
10:38 Imagine Dragons - It's Time
10:34 Panic! at the Disco - High Hopes
10:31 Selena Gomez - Hands to Myself
此代码获取数据,但我不知道如何将第二个 td 标签值保存在表中。当第二个td没有链接时,它不一定是一个链接,那么td标签中没有定义类。
正如@Denis V所说,不要使用正则表达式来解析html/xml内容,为此使用适当的库,如LibXML...
例:
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(file_get_contents('https://onlineradiobox.com/us/977todayshits/playlist'));
$xPath = new DOMXPath($dom);
$nodes = $xPath->query('//table[@class="tablelist-schedule"]/tbody/tr/td[2]');
foreach ($nodes as $node) {
echo $node->textContent . "n";
}
指纹。。。
比特犬 - 我们生命中的时间(壮举。
艾莉·古尔丁 - 靠近我(x 迪普洛壮举。 斯威·李(
Post Malone & Swae Lee - 向日葵
我们为什么不 - 8 个字母
NF - 谎言
。列表太长...