[英]PHP Simple HTML DOM Scrape External URL
我正在嘗試建立我的個人項目,但是在使用Simple HTML DOM類時有點卡住。
我想做的就是刮一個網站並檢索所有內容,它是與特定類匹配的內部html。
到目前為止,我的代碼是:
<?php
error_reporting(E_ALL);
include_once("simple_html_dom.php");
//use curl to get html content
$url = 'http://www.peopleperhour.com/freelance-seo-jobs';
$html = file_get_html($url);
//Get all data inside the <div class="item-list">
foreach($html->find('div[class=item-list]') as $div) {
//get all div's inside "item-list"
foreach($div->find('div') as $d) {
//get the inner HTML
$data = $d->outertext;
}
}
print_r($data)
echo "END";
?>
我所得到的只是帶有“ END”的空白頁,什么都沒有輸出。
似乎您的$ data變量在每次迭代中都分配了不同的值。 嘗試以下方法:
$data = "";
foreach($html->find('div[class=item-list]') as $div) {
//get all divs inside "item-list"
foreach($div->find('div') as $d) {
//get the inner HTML
$data .= $d->outertext;
}
}
print_r($data)
希望對您有所幫助。
我想,您可能想要這樣的東西
$url = 'http://www.peopleperhour.com/freelance-seo-jobs';
$html = file_get_html($url);
foreach ($html->find('div.item-list div.item') as $div) {
echo $div . '<br />';
};
這將為您提供類似的信息(如果您添加適當的樣式表,它將很好地顯示)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.