我所試圖做的是刮的旅行顧問頁 - 我有什麼,我從第一頁需要,然後我做另一個循環來從下一頁的內容,但當我嘗試將這些細節添加到現有數組中時,出於某種原因它不起作用。PHP簡單的HTML DOM解析器 - 結合兩種陣列
error_reporting(E_ALL);
include_once('simple_html_dom.php');
$html = file_get_html('http://www.tripadvisor.co.uk/Hotels-g186534-c2-Glasgow_Scotland-Hotels.html');
$articles = '';
// Find all article blocks
foreach($html->find('.listing') as $hotel) {
$item['name'] = $hotel->find('.property_title', 0)->plaintext;
$item['link'] = $hotel->find('.property_title', 0)->href;
$item['rating'] = $hotel->find('.sprite-ratings', 0)->alt;
$item['rating'] = explode(' ', $item['rating']);
$item['rating'] = $item['rating'][0];
$articles[] = $item;
}
foreach($articles as $article) {
echo '<pre>';
print_r($article);
echo '</pre>';
$hotel_html = file_get_html('http://www.tripadvisor.co.uk'.$article['link'].'/');
foreach($hotel_html->find('#MAIN') as $hotel_page) {
$article['address'] = $hotel_page->find('.street-address', 0)->plaintext;
$article['extendedaddress'] = $hotel_page->find('.extended-address', 0)->plaintext;
$article['locality'] = $hotel_page->find('.locality', 0)->plaintext;
$article['country'] = $hotel_page->find('.country-name', 0)->plaintext;
echo '<pre>';
print_r($article);
echo '</pre>';
$articles[] = $article;
}
}
echo '<pre>';
print_r($articles);
echo '</pre>';
這裏是所有的調試輸出,我得到:http://pastebin.com/J0V9WbyE
網址:http://www.4playtheband.co.uk/scraper/
*更好*使用SimpleXML的或的DomDocument。只是說。我知道這可能聽起來很蹩腳,因爲你不要求那樣做。所以我現在很沉默。 – hakre 2012-08-13 21:02:09
使用的Web刮的XML庫的問題是,這將是無法容忍這是無效的XML,這很可能是即使網站自稱是XHTML任何標記的。 simple_html_dom以類似瀏覽器的「標籤湯」的方式進行解析,因此可以製作出更強大的刮板。 – IMSoP 2012-08-20 14:54:15