【发布时间】:2016-12-23 19:49:49
【问题描述】:
我是抓取网站的新手,我有兴趣从该网站获取票价。 https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495 我在 p#price-selected-label.filters-selected-label 标签中看到了票价,但我似乎无法访问它。我尝试了一些东西并查看了一些教程,但要么返回空白,要么出现一些错误。代码基于http://blog.endpoint.com/2016/07/scrape-web-content-with-php-no-api-no.html
<?php
require('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495');
// creating an array of elements
$videos = [];
// Find top ten videos
$i = 1;
$videoDetails = $html->find('p#price-selected-label.filters-selected-label')-> innertext;
// $videoDetails = $html->find('p#price-selected-label.filters-selected-label',0);
echo $videoDetails;
/*
foreach ($html->find('li.expanded-shelf-content-item-wrapper') as $video) {
if ($i > 10) {
break;
}
// Find item link element
$videoDetails = $video->find('a.yt-uix-tile-link', 0);
// get title attribute
$videoTitle = $videoDetails->title;
// get href attribute
$videoUrl = 'https://youtube.com' . $videoDetails->href;
// push to a list of videos
$videos[] = [
'title' => $videoTitle,
'url' => $videoUrl
];
$i++;
}
var_dump($videos);
*/
【问题讨论】:
-
网站有时会使用 javascript 向网站填充内容。
-
你必须为此使用php吗?
-
不,我不需要使用 php,我只是最喜欢它,但如果它可以更好地工作,我也可以使用其他东西。谢谢
-
这种东西我一般用
PhantomJs,它会抓取整个页面,你可以用jquery选择你想要的元素并返回给php, -
报告空白页或“一些错误”不是有用问题的一部分。在您的问题中提供实际的错误消息。
标签: php html web-scraping