2012-06-25 31 views
2

我想將網站中的數據保存到mysql數據庫中。我能夠挽救大部分我想要拯救的東西,但我有一個特殊的問題。我提取的鏈接是保存,但我想鏈接在相同的行其他attributes.Below是我的CURL和MySQL查詢提取和保存到數據庫中的信息。鏈接保存與mysql數據庫中的其他數據分開

$target_url = "http://www.ucc.ie/modules/descriptions/BM.html"; 
$codeS = "BM"; 
$html = file_get_contents("http://www.ucc.ie/modules/descriptions/BM.html"); 
@$doc = new DomDocument(); 
@$doc->loadHtml($html); 
//discard white space 
@$doc->preserveWhiteSpace = false; 
$xpath = new DomXPath($doc); 

//Read through dd tags 
$options = $doc->getElementsByTagName('dd'); 

//Go into dd tags and look for all the links with class modnav 
$links = $xpath->query('//dd //a[@class = "modnav"]'); 

//Loop through and display the results for links 
foreach($links as $link){  
echo $link->getAttribute('href'), '<br><br>'; 
} 

foreach ($options as $option) { 

    $option->nodeValue; 
    echo "Node Value (Module name/title)= $option->nodeValue <br /><br /> <br />"; 

     // save both for each results into database 
$query3 = sprintf("INSERT INTO all_modulenames(code,module_name,description_link,gathered_from) 
    VALUES ('%s','%s','%s','%s')", 
    mysql_real_escape_string ($codeS), 
    mysql_real_escape_string($option->nodeValue), 
    mysql_real_escape_string($link->getAttribute('href')), 
    mysql_real_escape_string($target_url)); 
    mysql_query($query3) or die(mysql_error()."<br />".$query3); 

    } 
    echo "<br /> <br /> <br />"; 


Here is the table 
-- ---------------------------- 
-- Table structure for `all_modulenames` 
-- ---------------------------- 
DROP TABLE IF EXISTS `all_modulenames`; 
CREATE TABLE `all_modulenames_copy` (
`code` varchar(255) NOT NULL, 
`module_name` varchar(255) NOT NULL, 
`description_link` varchar(255) NOT NULL, 
`gathered_from` varchar(255) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of all_modulenames 
-- ---------------------------- 

所以,問題是「$鏈路>的getAttribute(‘href’屬性)」分別從其他內容保存在嘗試save.The鏈接保存,然後再接着是數據的保留其餘有些行是空的,但我試圖一次保存所有內容,即填充每行,然後移動到第二行,直到每個語句完成。我怎麼能這樣做?任何幫助,將不勝感激 !!

+0

查詢應該是一個循環 – 2012-06-25 00:43:46

+0

這裏還有一個額外的美元符號'mysql_real_escape_string($$鏈路>的getAttribute(」內href')),'這似乎不是故意的。這很可能會導致該字段爲空。 –

+0

嘿謝謝我在循環中查詢,我仍然得到同樣的結果。 @肖恩約翰遜和我糾正了$$的錯誤,但仍然得到相同的結果。 – user1444442

回答

1

未經測試的(所以需要調試),但我想辦法是這樣的:

...etc 
@$doc->preserveWhiteSpace = false; 

//Read through dd tags 
$options = $doc->getElementsByTagName('dd'); 

foreach ($options as $option) { 

    // Get the links and find the one with the right class 
    $href = ''; 
    $links = $option->getElementsByTagName('a'); 
    foreach ($link as $link) { 
     if ($link->hasAttribute('class') && $link->hasAttribute('href')) { 
      $aClasses = explode(' ', $link->getAttribute('class')); 
      if (in_array('modnav', $aClasses)) { 
        $href=$link->getAttribute('href'); 
      } 
     } 
    } 

    Insert in to SQL etc, $href is the link text belonging to the dd ... 
+0

嗨Robbie,感謝您的repply.I嘗試使用上面的代碼,但我得到了錯誤:**解析錯誤:語法錯誤,意外的')'**。這個錯誤實現了這一行:'$ aClasses = explode('',);'。有沒有解決這個問題的建議? – user1444442

+0

修復該行。你可能會得到其他錯誤,也需要調試 - 對不起,如果你這樣做,但它只是一個未經測試的建議(基於其他人無法回答的事實)。注意:我也編輯過$ href = $ link-> getAttribute('href');因爲這是錯的 – Robbie

+0

嗨羅比,非常感謝修復這個錯誤。我也修正了foreach語句。這兩個變量有相同的名字,並且在代碼的末尾增加了另一個右括號。非常感謝你的時間和精力,我會給它一個去,讓你知道我怎麼得到它。感謝 – user1444442