如何从其他网页获取内容并存储在数据库中

时间:2012-06-27 11:19:30

标签: php curl

地狱之友

我需要从这里获取比赛结果

"http://www.drf.com/race-results/BHP/USA/2012-06-23/D"

并希望存储在我的数据库中 我需要获取第1场比赛,第2场比赛,第3场等的所有记录

请建议我 我正在使用此代码但它显示我的整页我只想要特定的信息

      <?php
       $ch = curl_init();   
      //Fetch the timeline
         curl_setopt($ch, CURLOPT_URL, 'http://www.drf.com/race-results/BHP/USA/2012-06-24/D');
 //send data via $_GET
 //curl_setopt($ch, CURLOPT_GET, 0);

//do not return the header information
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);

//If SSL verification is needed. Delete if not needed
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);

    //Give me the data back as a string... Don't echo it.
      //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        //Warp 9, Engage!

       $content = curl_exec($ch);

       //Close CURL connection & free the used memory.

       curl_close($ch);
         ?>

4 个答案:

答案 0 :(得分:2)

我建议使用Goutte库。它将允许您使用记录良好的API来搜索和解析远程站点。您甚至可以关注链接并提交表单。

文档中的示例用法:

use Goutte\Client;

$client = new Client();

使用request()方法发出请求:

$crawler = $client->request('GET', 'http://www.symfony-project.org/');

该方法返回一个Crawler对象(Symfony \ Component \ DomCrawler \ Crawler)。

点击链接:

$link = $crawler->selectLink('Plugins')->link();
$crawler = $client->click($link);

基于CSS类提取数据并输出文本:

$nodes = $crawler->filter('.error_list');
if ($nodes->count())
{
  die(sprintf("Authentification error: %s\n", $nodes->text()));
}

printf("Nb tasks: %d\n", $crawler->filter('#nb_tasks')->text());

答案 1 :(得分:0)

你应该看看PHP dom解析器。 Link

解析HTML页面以获取所需数据并将其保存到数据库中。

祝你好运。

答案 2 :(得分:0)

Curl将返回网站的HTML代码,这是预期的。

转到实际网站,识别显示结果的div。然后使用PHP dom解析器或者甚至是字符串提取来提取特定部分的数据(容易但效率低,不建议使用)。

从部分中删除HTML标记并保存所需的数据

答案 3 :(得分:0)

使用PHP simplehtmldom解析器从HTML http://simplehtmldom.sourceforge.net/

中提取内容
相关问题