這應該工作,但它的混亂和可能的,如果你刮網站,會改變它的標記,這將影響到刮它會破壞:
$sites[0] = 'http://www.traileraddict.com/';
// use this if you want to retrieve more than one page:
// $sites[1] = 'http://www.traileraddict.com/trailers/2';
foreach ($sites as $site)
{
$ch = curl_init($site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
// ok, you have the whole page in the $html variable
// now you need to find the common div that contains all the review info
// and that appears to be <div class="info"> (I think you could use abstract aswell)
$title_start = '<div class="info">';
$parts = explode($title_start,$html);
// now you have an array of the info divs on the page
foreach($parts as $part){
// so now you just need to get your title and link from each part
$link = explode('<a href="/trailer/', $part);
// this means you now have part of the trailer url, you just need to cut off the end which you don't need:
$link = explode('">', $link[1]);
// this should give something of the form:
// overnight-2012/trailer
// so just make an absolute url out of it:
$url = 'http://www.traileraddict.com/trailer/'.$link[0];
// now for the title we need to follow a similar process:
$title = explode('<h2>', $part);
$title = explode('</h2>', $title[1]);
$title = strip_tags($title[0]);
// INSERT DB CODE HERE e.g.
$db_conn = mysql_connect('$host', '$user', '$password') or die('error');
mysql_select_db('$database', $db_conn) or die(mysql_error());
$sql = "INSERT INTO trailers(url, title) VALUES ('".$url."', '".$title."')"
mysql_query($sql) or die(mysql_error());
}
應該是這樣,現在你有一個鏈接和標題的變量,你可以插入到你的數據庫中。
免責聲明
我從我的頭在工作的頂部開始寫這個,所以我道歉,如果它不直接工作了蝙蝠,但讓我知道,如果沒有,我會嘗試進一步幫助。
此外,我知道這可以做得更聰明,使用更少的步驟,但這將涉及更多的思考我的部分和OP可以做到這一點,如果他們希望一旦他們已經瞭解我寫的代碼,因爲我會假設更重要的是他們明白我所做的並能夠自己編輯它。
此外,我會建議在晚上刮網站,以免給額外流量帶來負擔,並且我會建議您徵求該網站的許可,因爲如果他們抓到您,他們將能夠結束您的刮:(
要回答你的最後一點 - 在你可以使用cron作業設定的時間段運行此
我已經嘗試了一些PHP腳本(捲曲的file_get_contents),我在net.but找到。這些腳本只能獲取整個網頁而不具體數據我也不知道如何將這些數據保存到我的mysql數據庫中 – Eka 2012-03-01 12:09:57
在google上查詢phpquery或htmlsql或simplehtmldom – Sarfraz 2012-03-01 12:13:27