1
使用PHP,我試圖得到所有在網頁上列出的東西的價格。有人可以幫我把這個寫得對嗎?我很確定我正在編寫foreach行,因爲某些事情需要處理$ listing?此外,我不確定長部分的寫法是否正確,將名稱和價格放入$ name和$ price變量中?在PHP我的foreach循環與數組似乎寫錯了,但我不知道?
<?php
$all = file_get_contents('http://shop.hobbylobby.com/search/?keyword=cricut%20cartridge&match=AND&F_Sort=2&F_ALL=Y');
echo $all;
$name = array();
$price = array();
foreach($all as $listing) {
$name[] = $listing.GetElementsByClassName("listingWrpr").GetElementsByTagName("h3").innerhtml;
$price[] = $listing.GetElementsByClassName("listingWrpr").GetElementsByClassName("item price").innerhtml;
}
print_r($name);
print_r($price);
?>
問題不在於foreach,而是嘗試在PHP中使用Javascript--兩者可以互相交流,但不要混用。您應該使用PHP DOM方法來提取信息 –
我怎樣才能做到這一點,沒有6周和智商200? – user1140067
你也可以使用simple_html_Dom_Parser或phpQuery –