【问题标题】:php crawler(to crawl single website)php爬虫(爬取单个网站)
【发布时间】:2012-09-24 03:06:57
【问题描述】:

我正在做爬虫项目,我需要你的帮助,这是我的第一个项目。任务是从“http://justdial.com”获取数据。例如,我想获取城市名称(班加罗尔)、类别(酒店)、酒店名称、地址和电话号码。

我已经编写了一个代码来从它的“id”中获取标签内容,就像我从这里获取地址一样:

<?php

$url="http://www.justdial.com/Bangalore/hotels";  
$original_file = file_get_contents("$url");
$stripped_file = strip_tags($original_file, "<div>");

$newlines="'<div class=\"logoDesc\">(.*?)</div>'si";
$newlines=preg_replace('#<div(?:[^>]*)>.</div>#u','',$newlines);

preg_match_all("$newlines", $stripped_file, $matches);


//DEBUGGING

  //$matches[0] now contains the complete A tags; ex: <a href="link">text</a>
  //$matches[1] now contains only the HREFs in the A tags; ex: link

  header("Content-type: text/plain"); //Set the content type to plain text so the print below is easy to read!
 $path= ($matches);

 print_r($path); //View the array to see if it worked
?>

现在的问题是,我想将标签与内容分开并将其存储在数据库中。从数据库到excel表。请帮帮我。

【问题讨论】:

  • 你的意思是strip_tags()
  • 路径包含什么?请给我们看一个转储。您是否尝试过任何数据库代码?是否需要从数据库-> excel,或者可以同时生成excel表?它必须是 xls,还是 csv 就足够了?
  • 你的意思是MySQLfputcsv

标签: php parsing html-parsing web-crawler


【解决方案1】:

您不应该使用正则表达式来解析 HTML。您应该使用DomDocument 之类的东西。使用中的小例子:

<?php
   $str = '<h1>T1</h1>Lorem ipsum.<h1>T2</h1>The quick red fox...<h1>T3</h1>... jumps over the lazy brown FROG';
   $DOM = new DOMDocument;
   $DOM->loadHTML($str);

   //get all H1
   $items = $DOM->getElementsByTagName('h1');

   //display all H1 text
   for ($i = 0; $i < $items->length; $i++)
        echo $items->item($i)->nodeValue . "<br/>";
?>

【讨论】:

  • 我已经使用了html解析来解析php的内容。这是代码。
  • 你好@wayne,我已经包含了html解析器来解析php的内容。我不想用数据库,我想用记事本。在运行“justdial.com”页面时,数据必须存储在记事本中,然后从记事本存储到 Excel 工作表中。
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多