2012-03-01 30 views
7

任何人都可以告訴我如何使用curl或file_get_contents從網站下載特定數據,然後將這些特定數據保存到我的mysql數據庫。我想從本網站獲得最新的電影作品http://www.traileraddict.com/,我想將它保存在我的數據庫中(每天都在;我的網站上會顯示這段文字和html鏈接)。我只需要在文本和HTML鏈接。(在PIC高亮顯示)如何使用cURL從網站獲取特定數據,然後將其保存我的數據庫使用php

enter image description here

我已經到處搜尋,但我沒有發現任何有用的教程。我有兩個要問的主要問題

1)如何使用cURL或file_get_contents獲取特定數據。

2)我怎樣才能具體內容保存到我的MySQL數據庫表(在一列文字和鏈接的另一列)

+0

我已經嘗試了一些PHP腳本(捲曲的file_get_contents),我在net.but找到。這些腳本只能獲取整個網頁而不具體數據我也不知道如何將這些數據保存到我的mysql數據庫中 – Eka 2012-03-01 12:09:57

+0

在google上查詢phpquery或htmlsql或simplehtmldom – Sarfraz 2012-03-01 12:13:27

回答

13

使用捲曲:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.something.com'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$content = curl_exec($ch); 

然後您可以將元素加載到一個DOM對象並解析特定數據的dom。您也可以嘗試使用搜索字符串解析數據,但在HTML上使用正則表達式是非常令人不滿的。

$dom = new DOMDocument(); 
$dom->loadHTML($content); 

// Parse the dom for your desired content 
+0

如何獲取來自網站的具體數據?而不是整個網頁 – Eka 2012-03-01 12:12:14

+0

您首先需要獲取整個網頁,然後通過將其加載到DomDocument類中並導航到所需的節點來解析所需的信息。 – SS44 2012-03-01 12:12:57

+0

我是新來的DOM,但感謝您的回覆...我現在會試試這個 – Eka 2012-03-01 12:18:56

8

這應該工作,但它的混亂和可能的,如果你刮網站,會改變它的標記,這將影響到刮它會破壞:

$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作業設定的時間段運行此

+0

嘿感謝您花寶貴的時間來回答我的查詢,也給我的PHP腳本... – Eka 2012-03-01 14:19:46

+0

沒問題,它工作正常? – martincarlin87 2012-03-01 15:26:13

+0

我沒有試過這個代碼..我有簡單的html dom..its簡單..但我很高興你給了這個腳本 – Eka 2012-03-02 08:38:14

相關問題