0

我刚刚开始使用 PHP,我想抓取一个我不能抓取的小页面,我尝试执行“PREG_MATCH_ALL”,但它没有得到我想要的结果。基本上我想从这里抓取 youtube 视频链接仅限:https ://gdata.youtube.com/feeds/api/standardfeeds/most_shared - 刮掉所有这些,然后再使用它们。

我尝试使用以下失败的代码;

<?php
    $data = file_get_contents('https://gdata.youtube.com/feeds/api/standardfeeds/most_shared');
    preg_match_all("/src='(.+?)'>/", $data, $links);
    $link_out = $links[0][0];
    echo $link_out;
?>

我是 PHP 新手,请大家帮忙。

谢谢

4

3 回答 3

2

由于 feed 是 XML,您可以使用 PHP 的SimpleXMLElement来获取数据。

<?php
$xml = new SimpleXMLElement(
    'https://gdata.youtube.com/feeds/api/standardfeeds/most_shared',
    null,
    true
);

foreach($xml->entry as $entry) {
    echo $entry->content['src'], PHP_EOL;
}

/*
    https://www.youtube.com/v/IjWc43FCYlg?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/Xw1C5T-fH2Y?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/Kq0_dGKx4Os?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/gbcBYs0ljI0?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/78juOpTM3tE?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/OOiZ-5DqwYI?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/zjz614QVyfQ?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/h15m87WsCHQ?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/SXKOTdyOUBg?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/BRAM8MpqIeA?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/5yB3n9fu-rM?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/NAOo9SnzRH8?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/0KtILkzC-1g?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/kWSIFh8ICaA?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/Mi6AhogZCeg?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/kWuIGAZ1x2I?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/lKY5fmDGVLs?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/C94PaCtqOk4?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/V-fL8zopddI?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/UWlzMIl7E48?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/mcw6j-QWGMo?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/-RSDaRttpzk?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/8_RDx4skTp4?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/7YDWdv9kR0M?version=3&f=standard&app=youtube_gdata
    https://www.youtube.com/v/m96tYpEk1Ao?version=3&f=standard&app=youtube_gdata
*/

安东尼。

于 2013-06-21T07:49:37.507 回答
1

试试这个预赛:

preg_match_all("/src='([^']+)'/si", $data, $links);

并显示结果:

echo "<pre>";
print_r($links);
于 2013-06-21T07:44:54.723 回答
1
<?php
$data = file_get_contents('https://gdata.youtube.com/feeds/api/standardfeeds/most_shared');
preg_match_all("/src='(.+?)'\/>/", $data, $links);
print_r($links[1]);

您忘记匹配锚标签的结束 / 。

于 2013-06-21T07:46:00.947 回答