php抓取网页信息

本文介绍了一种使用PHP简单HTML DOM库从影视网站抓取影视名称、链接及相关详细信息的方法,并展示了如何将抓取到的数据以表格形式展示。
index.php



<?php
include_once 'simple_html_dom.php';
//获取html数据转化为对象
$html = file_get_html('http://paopaotv.com/tv-type-id-5-pg-1.html');
//A-Z的字母列表每条数据是在id=letter-focus 的div内class= letter-focus-item的dl标签内,用find方法查找即为 
$listData=$html->find("#letter-focus .letter-focus-item");//$listData为数组对象

foreach($listData as$key=>$eachRowData){
  $filmName=$eachRowData->find("dd span",0)->plaintext;//获取影视名称

  $filmUrl=$eachRowData->find("dd a",0)->href;//获取dd标签下影视对应的地址

  //获取影视的详细信息
  $filmInfo=file_get_html("http://paopaotv.com".$filmUrl);
  $filmDetail=$filmInfo->find(".info dl");
  foreach($filmDetail as $film){
    $info=$film->find("dd");
    $row=null;
    foreach($info as $childInfo){
      $row[]=$childInfo->plaintext;
    }
    $cate[$key][]=join(",",$row);//将影视的信息存放到数组中
  }
}
?>

<table border="1px solid red" width="100%">
  <tr>
    <th>主演</th>
    <th>状态</th>
    <th>类型</th>
    <th>地区</th>
    <th>标签</th>
    <th>导演</th>
    <th>时间</th>
    <th>年份</th>
  </tr>


<?php foreach ($cate as $val){
echo "<tr>";
  for ($i=0; $i < count($val)-1; $i++) { 

    echo "<td>".$val[$i]."</td>";
  }    
echo "</tr>";
} ?>

</table>

<?php 
echo "<pre>";
print_r($cate);
echo "</pre>";

?>

相关代码下载:files.cnblogs.com/files/qhorse/getspider.rar

 

转载于:https://www.cnblogs.com/qhorse/p/5101638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值