我刚开始学习编程大约6个月前,我真的深入了解Objective-C。不幸的是,我不知道任何程序员IRL会反复提出一般性问题。
当人们编写搜索网站信息并将其发回的程序时,会使用哪些语言?例如,如果我想编写一个程序来搜索weather.com,查看给定位置中过去30天的每日温度,然后将其作为一个NSArray或NSDictionary发送回来,我该怎么做?我可以在Objective C中执行此操作,还是使用超高级脚本语言?如果我可以在Objective-C中完成,有人可以链接到可能让我开始学习这类东西的教程或地方吗? (我真的不知道这种编程的术语,所以我的谷歌搜索一直没有用。)
答案 0 :(得分:1)
我最常用PHP和MySQL与CURL
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();
}
?>
答案 1 :(得分:0)
那么,您想知道如何编写服务器端代码吗?好吧,理论上你可以用任何你想要的东西来写。我也向你保证,这不是“超级先进”。
您可能会发现PHP入门最简单。 W3schools.com有fine tutorial。
答案 2 :(得分:0)
您所描述的是crawler(例如Google)。
任何能够发送HTTP请求和接收响应的语言都可以执行此操作(大多数语言)。
如果您不想从头开始编写代码,请尝试下载open source crawler框架,以便自定义插件解析生成的HTML。
对于您的示例,您将告诉抓取工具您希望它抓取哪个网站(即您的天气网站),必要时添加URI约束,并创建自定义插件以从其响应的HTML中解析天气数据。然后,您可以根据需要保存该数据。