2010-11-16 80 views
0

我剛開始學習大約6個月前的編程,我真的深入到Objective-C深入。不幸的是,我不知道任何程序員IRL反彈一般問題。一般互聯網「刮」問題

當人們寫,將搜索一個網站信息的程序正在使用,然後將其發回什麼語言?例如,如果我想編寫一個程序,可以在給定位置搜索weather.com過去30天的日常溫度,然後將其發送回來,例如... NSArray或NSDictionary,我該怎麼做?我可以在Objective C中做到這一點,還是那種超級高級的腳本語言?如果我可以在Objective-C中做到這一點,有人可以鏈接到一個教程或地方,可以讓我開始學習這種類型的東西? (我真的不知道這種類型的編程術語,所以我的谷歌搜索一直徒勞的。)

回答

1

我最常使用PHP和MySQL,捲曲

http://en.wikipedia.org/wiki/CURL

你可以做一些有趣的事情,就像搜索引擎結果頁面查詢等

這裏是一個履帶我用源。爲了匿名的緣故,我已經刪除了一些部分,但這是一個很好的幾乎可行的例子。如果需要的話,我可以幫你解決問題。

<?php 
class Crawler { 

    protected $markup = ''; 
    protected $uri = ''; 

    protected $db_location = "localhost"; 
    protected $db_username = "***"; 
    protected $db_password = "***"; 
    protected $db_name = "***"; 

    public function __construct() { 
    ini_set('memory_limit', -1); 
    } 

    public function getMarkup() { 
    $markup = ""; 
    $markup = @file_get_contents($this->uri); 
    return $markup; 
    } 

    public function get($type) { 
    $method = "_get_{$type}"; 
    if (method_exists($this, $method)){ 
     return call_user_method($method, $this); 
    } 
    } 

    protected function db_query($query) { 
    $connection = mysql_connect($this->db_location,$this->db_username,$this->db_password) or die(mysql_error()); 
    mysql_select_db($this->db_name,$connection) or die(mysql_error()." >> ".$query); 

    //echo $query."<br/>"; //for debugging 

    $result = mysql_query($query,$connection) or die (mysql_error()." >> ".$query); 


    $i = 0; 

    if($result != 1) 
     { 
      while ($data_array = mysql_fetch_array($result)) 
       { 
        foreach($data_array as $key => $value) 
         { 
          $tableArray[$i][$key] = stripslashes($data_array[$key]); 
         } 

        $i++; 
       } 

      return $tableArray; 
     } 
    } 

    protected function db_insert($table,$array) { 
     $tableArray = $this->db_query("show columns from ".$table); 

     $inputString = ""; 

     foreach($tableArray as $key => $value) 
      { 
       if (array_key_exists($value[0], $array) && $value[0]) { 
        $inputString .= "'".addslashes($array[$value[0]])."', "; 
       } else { 
        $inputString .= "'', "; 
       } 
      } 

     $inputString = substr($inputString, 0, -2); 
     $this->db_query("insert into $table values(".$inputString.")"); 

     return mysql_insert_id(); 
    } 

    protected function _get_data() { 
     //$scrape['id'] = $this->get('id'); 
     $scrape['name'] = $this->get('name'); 
     $scrape['tags'] = $this->get('tags'); 
     $scrape['stat_keys'] = $this->get('stat_keys'); 
     $scrape['stat_values'] = $this->get('stat_values'); 

     foreach($scrape['stat_values'] as $key => $value) { 
      $scrape['stat_values'][$key] = trim($scrape['stat_values'][$key]); 

      if(strpos($value,"<h5>Featured Product</h5>")) { 
       unset($scrape['stat_values'][$key]); 
      } 
      if(strpos($value,"<h5>Featured Company</h5>")) { 
       unset($scrape['stat_values'][$key]); 
      } 
      if(strpos($value,"<h5>Featured Type</h5>")) { 
       unset($scrape['stat_values'][$key]); 
      } 
      if(strpos($value,"sign in")) { 
       unset($scrape['stat_values'][$key]); 
      } 
      if(strpos($value,"/100")) { 
       unset($scrape['stat_values'][$key]); 
      } 
     } 

     if(sizeof($scrape['tags']) > 0 && is_array($scrape['tags'])) { 
      foreach($scrape['tags'] as $tag) { 
       $tag_array[$tag] = $tag_array[$tag] + 1; 
      } 

      $scrape['tags'] = $tag_array; 

      foreach($scrape['tags'] as $key => $tag_count) { 
       $scrape['tags'][$key] = $tag_count - 1; 
      } 
     } 

     $scrape['stat_values'] = array_merge(array(),$scrape['stat_values']); 

     return $scrape; 
    } 

    protected function _get_images() { 
    if (!empty($this->markup)){ 
     preg_match_all('/<img([^>]+)\/>/i', $this->markup, $images);   
     return !empty($images[1]) ? $images[1] : FALSE; 
    } 
    } 

    protected function _get_links() { 
    if (!empty($this->markup)){ 
     preg_match_all('/<a([^>]+)\>(.*?)\<\/a\>/i', $this->markup, $links); 
     return !empty($links[1]) ? $links[1] : FALSE; 
    } 
    } 

    protected function _get_id() { 
    if (!empty($this->markup)){ 
     preg_match_all('/\/wine\/view\/([^`]*?)-/', $this->markup, $links); 
     return !empty($links[1]) ? $links[1] : FALSE; 
    } 
    } 


    protected function _get_grape() { 
    if (!empty($this->markup)){ 
     preg_match_all('/ class="linked" style="font-size: 14px;">([^`]*?)<\/a>/', $this->markup, $links); 
     return !empty($links[1]) ? $links[1] : FALSE; 
    } 
    } 
} 

if($_GET['pass'] == "go") { 
    $crawl = new Crawler(); 
    $crawl->go(); 
} 
?> 
0

所以,你想知道如何編寫服務器端代碼?那麼,理論上你可以隨心所欲地寫出來。我也向你保證它不是「超級先進」的。

您可能會發現最簡單的上手PHP。 W3schools.com有一個fine tutorial

0

你所描述是crawler(如谷歌)。

具有發送HTTP請求和接收響應可以做到這一點(這是大多數語言)的能力,任何一種語言。

如果您不介意從頭開始編寫此代碼,請嘗試下載一個open source crawler框架,該框架允許自定義插件解析生成的HTML。

舉個例子,你會告訴爬蟲你想要它抓取什麼網站(如你的天氣網站),如果需要添加URI約束,並創建一個自定義插件來解析HTML響應的天氣數據。然後,您可以保存該數據,但是您認爲合適。