2010-11-16 81 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响应的天气数据。然后,您可以保存该数据,但是您认为合适。