当前位置: 首页 > news >正文

phpQuery对数据信息的采集进一步学习

前提:需要下载:phpQuery/phpQuery.php

链接:http://www.cnblogs.com/wuheng1991/p/5145398.html

1.对于规则的部分

<?php
header('Content-Type:text/html;charset=UTF-8');
include './phpQuery/phpQuery.php';
set_time_limit(10000);
$id = isset($_GET['id']) ? intval($_GET['id']) : 1;

if($id > 46){
   echo "finish!";
   exit;
}
// exit;
echo "当前 id=".$id;
echo "<br/>";
$url = "http://www.genetex.com/web/Search/SearchList.aspx?&Category=494&Page=".$id."&SpecialGroupID=240";
echo "当前的URL:";
echo $url."<br/>";

phpQuery::newDocumentFile($url);

$artList = pq(".sear_list");
$count = count($artList);
// var_dump($count);
$i = 0;
foreach($artList as $li){
    $url = '';
    $tr = '';
    $tr = pq($li)->eq(0)->find("li")->eq(1)->find("a")->attr('href');
    $tr = trim($tr);
    // var_dump($tr);
    $url = "http://www.genetex.com/";
    $arr = explode("/",$tr);
    // exit;
    $end = trim($arr[2]);
    
    $rel_url = $url.$end;
    $rel_url = $rel_url."\r\n";
    file_put_contents('url.txt',$rel_url,FILE_APPEND); //用于装载执行失败的数据
    echo '<br/>';
     
}

?>

<script>
function JumpUrl(){
    location.href='?id=<?php echo ($id+1);?>';
}
setTimeout(JumpUrl,0);
</script>

2.对于不规则的部分。(通过产品链接,得到产品相关信息)

<?php
header('Content-Type:text/html;charset=UTF-8');
include './phpQuery/phpQuery.php';
set_time_limit(10000);
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

$arr = file('./url.txt');
// var_dump($arr);

if($id > 453){
   echo "finish!";
   exit;
}

$curr_url = trim($arr[$id]);
unset($arr);
var_dump($curr_url);
echo '<br/>';
echo '<hr/>';
phpQuery::newDocumentFile($curr_url);

$arrAll = array();

$artList = pq(".table_style_1");
$app = pq(".sear_list_2")->eq(0)->find("span")->eq(1)->html();
$app = trim($app);
echo "Application Information:".$app;
echo '<hr/>';
$arrAll['xing_num']= '';
$arrAll['reference'] = '';
// var_dump($artList);
foreach($artList as $key =>$li){

    if($key == 0){

    
    
    $tr1 = '';  ####  Catalog Number,Full Name,Storage Buffer
    $tr2 = '';  ####  Catalog Number,Full Name,Storage Buffer 对应的属性值
    $tr3 = '';  ####  Product Name , Product Description,Storage Instruction
    $tr4 = '';  ####  Product Name , Product Description,Storage Instruction 对应的属性值
    $tr5 = '';  ####  Applications,Background, Notes
    $tr6 = '';  ####  Applications,Background, Notes 对应的属性值

    $tr1 = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(0)->html();
    $tr1 = trim($tr1);
    $tr1 = str_replace("\r\n","",$tr1);

    $tr2 = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->html();
    $tr2 = trim($tr2);


    $tr2_a = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->find(".recommend")->eq(0)->html();
    $tr2_a = trim($tr2_a);
    // $tr2_a = str_replace("\r\n","",$tr2_a);

    $tr2_xx = '';  ###星星
    $tr2_xx = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->find(".recommend")->eq(0)
    ->find("div")->eq(1)->html();
    $tr2_xx = strip_tags($tr2_xx);
    $tr2_xx = str_replace("\r\n","",$tr2_xx);
    $tr2_xx = str_replace("(","",$tr2_xx);
    $tr2_xx = str_replace(")","",$tr2_xx);
    $tr2_xx = str_replace(" ","",$tr2_xx);

    $arrAll['xing_num'] = $tr2_xx;

    // echo "<hr/>";
    // echo var_dump($tr2_xx);
    // echo "<hr/>";
    $tr2_yy = '';  ### refercence
    $tr2_yy = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->find(".recommend")->eq(0)
    ->find("div")->eq(2)->html();
    $tr2_yy = strip_tags($tr2_yy);
    $tr2_yy = str_replace("\r\n","",$tr2_yy);
    $tr2_yy = str_replace("(","",$tr2_yy);
    $tr2_yy = str_replace(")","",$tr2_yy);
    $tr2_yy = str_replace(" ","",$tr2_yy);

    $arrAll['reference'] = $tr2_yy;
    // echo "<hr/>";
    // echo var_dump($tr2_yy);
    // echo "<hr/>";





    $tr2 = str_replace($tr2_a,"",$tr2);
    $tr2 = strip_tags($tr2);
    $tr2 = str_replace(" ","",$tr2);
    $tr2 = str_replace("\r\n","",$tr2);
    $tr2 = trim($tr2);

    $tr3 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(0)->html();
    $tr3 = trim($tr3);
    $tr3 = str_replace("\r\n","",$tr3);
    
    if($key == 0){
    $tr4 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(1)->find('strong')->eq(0)->html();
    }else{
    $tr4 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(1)->html();
    }
    // $tr4 = trim($tr4);
    // $tr4 = str_replace(" ","",$tr4);
    $tr4 = str_replace("\r\n","",$tr4);
    $tr4 = trim($tr4);

    $tr5 = pq($li)->eq(0)->find("tr")->eq(2)->find("td")->eq(0)->html();
    $tr5 = trim($tr5);
    $tr5 = str_replace("\r\n","",$tr5);

    $tr6 = pq($li)->eq(0)->find("tr")->eq(2)->find("td")->eq(1)->html();
    $tr6 = str_replace(" ","",$tr6);
    $tr6 = str_replace("\r\n","",$tr6);
    $tr6 = trim($tr6);

    // echo $tr1."  ;  ".$tr2."   ;     ".$tr3."   ;   ".$tr4."  ;   ".$tr5." ;  ".$tr6;
    // echo '<hr/>';
    // echo '<br/>';
 }
    // exit;
}





$arrAll['Application_Information'] = $app;
$arrAll['catalog_number'] = $tr2;
$arrAll['product_name'] = $tr4;
$arrAll['applications'] = $tr6;

$arrAll['position_controls'] = "";    

$art = pq("#tab1 table");
// var_dump($art);
foreach($art as $k =>$v){

if($k == 0){
    // var_dump($v);
for($ii = 0;$ii < 5;$ii ++){
$a_1 = '';
$a_1 = pq($v)->eq(0)->find("tr")->eq(7+$ii)->find("td")->eq(0)->html();
$a_1 = trim($a_1);
if($a_1 == "Positive Controls"){

$a_2 = pq($v)->eq(0)->find("tr")->eq(7+$ii)->find("td")->eq(1)->html();    
$a_2 = trim($a_2);
$arrAll['position_controls'] = $a_2;

} 

}

// var_dump($a_1);
// exit;
}

}

// var_dump($arrAll);
$arrAll['full_name'] = '';
$arrAll['product_description'] = '';
$arrAll['synonyms'] = '';
$arrAll['host'] = '';
$arrAll['clonality'] = '';

$arrAll['isotype'] = '';
$arrAll['species_reactivity'] = '';
$arrAll['conjugation'] = '';



// echo '<br/>';
// echo '<hr/>';

foreach($artList as $key =>$li){

    if($key == 1){

    
    
    $tr1 = '';  ####  Catalog Number,Full Name,Storage Buffer
    $tr2 = '';  ####  Catalog Number,Full Name,Storage Buffer 对应的属性值
    $tr3 = '';  ####  Product Name , Product Description,Storage Instruction
    $tr4 = '';  ####  Product Name , Product Description,Storage Instruction 对应的属性值
    $tr5 = '';  ####  Applications,Background, Notes
    $tr6 = '';  ####  Applications,Background, Notes 对应的属性值

    $tr1 = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(0)->html();
    $tr1 = trim($tr1);
    $tr1 = str_replace("\r\n","",$tr1);

    $tr2 = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->html();
    $tr2 = trim($tr2);
    

    $tr2_a = pq($li)->eq(0)->find("tr")->eq(0)->find("td")->eq(1)->find(".recommend")->eq(0)->html();
    $tr2_a = trim($tr2_a);
    // $tr2_a = str_replace("\r\n","",$tr2_a);

    $tr2 = str_replace($tr2_a,"",$tr2);
    $tr2 = strip_tags($tr2);
    // $tr2 = str_replace(" ","",$tr2);
    $tr2 = str_replace("\r\n","",$tr2);
    $tr2 = trim($tr2);

    $tr3 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(0)->html();
    $tr3 = trim($tr3);
    $tr3 = str_replace("\r\n","",$tr3);
    
    if($key == 0){
    $tr4 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(1)->find('strong')->eq(0)->html();
    }else{
    $tr4 = pq($li)->eq(0)->find("tr")->eq(1)->find("td")->eq(1)->html();
    }
    // $tr4 = trim($tr4);
    // $tr4 = str_replace(" ","",$tr4);
    $tr4 = str_replace("\r\n","",$tr4);
    $tr4 = trim($tr4);
    
    for($jj = 0;$jj < 3;$jj ++){
        $tr5 = '';
        $tr5 = pq($li)->eq(0)->find("tr")->eq(2+$jj)->find("td")->eq(0)->html();
           $tr5 = str_replace("\r\n","",$tr5);
           $tr5 = trim($tr5);
           if($tr5 == "Synonyms"){

               $tr6 = pq($li)->eq(0)->find("tr")->eq(2+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr6 = str_replace("\r\n","",$tr6);
            $tr6 = trim($tr6);
            $arrAll['synonyms'] = $tr6;
           }

    }

       ####  host  #####
     
      for($jj = 0;$jj < 4;$jj ++){
        $tr7 = '';
        $tr7 = pq($li)->eq(0)->find("tr")->eq(3+$jj)->find("td")->eq(0)->html();
           $tr7 = str_replace("\r\n","",$tr7);
           $tr7 = trim($tr7);
           if($tr7 == "Host"){

               $tr8 = pq($li)->eq(0)->find("tr")->eq(3+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr8 = str_replace("\r\n","",$tr8);
            $tr8 = trim($tr8);
            $arrAll['host'] = $tr8;
           }

    }

    ##### clonality ####
      for($jj = 0;$jj < 4;$jj ++){
        $tr9 = '';
        $tr9 = pq($li)->eq(0)->find("tr")->eq(4+$jj)->find("td")->eq(0)->html();
           $tr9 = str_replace("\r\n","",$tr9);
           $tr9 = trim($tr9);
           if($tr9 == "Clonality"){

               $tr10 = pq($li)->eq(0)->find("tr")->eq(4+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr10 = str_replace("\r\n","",$tr10);
            $tr10 = trim($tr10);
            $arrAll['clonality'] = $tr10;
           }

    }

    #### isotype ######
     for($jj = 0;$jj < 4;$jj ++){
        $tr11 = '';
        $tr11 = pq($li)->eq(0)->find("tr")->eq(5+$jj)->find("td")->eq(0)->html();
           $tr11 = str_replace("\r\n","",$tr11);
           $tr11 = trim($tr11);
           if($tr11 == "Isotype"){

               $tr12 = pq($li)->eq(0)->find("tr")->eq(5+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr12 = str_replace("\r\n","",$tr12);
            $tr12 = trim($tr12);
            $arrAll['isotype'] = $tr12;
           }

    }

    #### species_reactivity #####

     for($jj = 0;$jj < 6;$jj ++){
        $tr13 = '';
        $tr13 = pq($li)->eq(0)->find("tr")->eq(6+$jj)->find("td")->eq(0)->html();
           $tr13 = str_replace("\r\n","",$tr13);
           $tr13 = trim($tr13);
           if($tr13 == "Species Reactivity"){

               $tr14 = pq($li)->eq(0)->find("tr")->eq(6+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr14 = str_replace("\r\n","",$tr14);
            $tr14 = trim($tr14);
            $arrAll['species_reactivity'] = $tr14;
           }

    }

    ######## conjugation ######
      for($jj = 0;$jj < 6;$jj ++){
        $tr15 = '';
        $tr15 = pq($li)->eq(0)->find("tr")->eq(7+$jj)->find("td")->eq(0)->html();
           $tr15 = str_replace("\r\n","",$tr15);
           $tr15 = trim($tr15);
           if($tr15 == "Conjugation"){

               $tr16 = pq($li)->eq(0)->find("tr")->eq(7+$jj)->find("td")->eq(1)->html();
            // $tr6 = str_replace(" ","",$tr6);
            $tr16 = str_replace("\r\n","",$tr16);
            $tr16 = trim($tr16);
            $arrAll['conjugation'] = $tr16;
           }

    }




    // echo $tr1."  ;  ".$tr2."   ;     ".$tr3."   ;   ".$tr4."  ;   ".$tr5." ;  ".$tr6;
    // echo '<hr/>';
    // echo '<br/>';
 }
    // exit;
}


$arrAll['full_name'] = $tr2;
$arrAll['product_description'] = $tr4;
$arrAll['researchArea'] = "";

// echo '<br/>';
// echo '<hr/>';

foreach($artList as $key =>$li){
    if($key == 2){

      $tr17 = pq($li)->eq(0)->find("tr")->eq(3)->find("td")->eq(0)->html();
      $tr17 = trim($tr17);
      $tr17 = str_replace("\r\n","",$tr17);

      $tr18 = pq($li)->eq(0)->find("tr")->eq(4)->find("td")->eq(0)->html();
     
      $tr18 = strip_tags($tr18);
       $tr18 = str_replace("More Hide","",$tr18);
       $tr18 = str_replace("\r\n","",$tr18);
       $tr18 = trim($tr18);
      // echo  $tr18;
      // echo '<br/>';
      // echo '<hr/>';
    }
}

// echo '<br/>';
// echo "★★★★☆";
// echo '<br/>';
$arrAll['researchArea'] = $tr18;
$arrAll['url'] = $curr_url;
var_dump($arrAll);


$html = "";
$html .= $arrAll['Application_Information']."\t".$arrAll['catalog_number']."\t".$arrAll['product_name']."\t";
$html .= $arrAll['applications']."\t".$arrAll['position_controls']."\t".$arrAll['full_name']."\t";
$html .= $arrAll['product_description']."\t".$arrAll['synonyms']."\t".$arrAll['host']."\t";
$html .= $arrAll['clonality']."\t".$arrAll['isotype']."\t".$arrAll['species_reactivity']."\t";
$html .= $arrAll['conjugation']."\t".$arrAll['researchArea']."\t".$arrAll['xing_num']."\t";
$html .= $arrAll['reference']."\r\n";

echo $html;
echo '<br/>';
// file_put_contents('info.txt',$html,FILE_APPEND); //用于装载执行失败的数据
$fp = fopen('file.csv', 'a');
fputcsv($fp,$arrAll);

unset($arrAll);
unset($artList);
?>
<script>
function JumpUrl(){
    location.href='?id=<?php echo ($id+1);?>';
}
setTimeout(JumpUrl,0);
</script>

3.对知识点的补充

a.使用phpQuery采集数据,实际上是通过链接地址,找到源码,再将源码转换为jQuery语法。然后通过jQuery语法获得数据

b.对于规则的部分,采集数据较容易。

c.对于不规则的部分,需要进行判断,较为复杂些,不过如果jQuery学的好,这也不是难事。

d.对函数:fputcsv() 的学习总结。如:

$fp = fopen('file.csv', 'a');
fputcsv($fp,$arrAll);

其中,$arrAll是一维数组。fputcsv(),会将函数的值写入到csv文件中,一次写一行,如果数组为空,则不会写入,一旦数组中的元素不为空,就可以写入。

参看:http://php.net/manual/zh/function.fputcsv.php

 

相关文章:

  • Redis底层数据结构
  • 用户口令复杂度策略设置银河麒麟
  • Redis的过期策略
  • 第三周作业 产品同质化问题
  • Redis分布式锁
  • LLVM3.8停止了旧Windows版本,取消Autoconf,改进Clang
  • Redis的I/O多路复用
  • mysql之select(一)
  • 多线程的 Redis
  • python中给程序加锁之fcntl模块的使用
  • 【VMCloud云平台】拥抱Docker(二)配置第一个容器
  • Redis持久化方式
  • Redis哨兵(Sentinel)模式
  • 倾斜的PDF页面怎样通过PDF Transformer+修正
  • 谈谈你对 MVC 模式的理解?
  • SegmentFault for Android 3.0 发布
  • [译] 怎样写一个基础的编译器
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【Leetcode】104. 二叉树的最大深度
  • JavaScript对象详解
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • mysql 数据库四种事务隔离级别
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • PHP变量
  • Vue官网教程学习过程中值得记录的一些事情
  • Vue--数据传输
  • 笨办法学C 练习34:动态数组
  • 初探 Vue 生命周期和钩子函数
  • - 概述 - 《设计模式(极简c++版)》
  • 关于使用markdown的方法(引自CSDN教程)
  • 微信小程序开发问题汇总
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • # 飞书APP集成平台-数字化落地
  • #ubuntu# #git# repository git config --global --add safe.directory
  • #微信小程序(布局、渲染层基础知识)
  • (06)金属布线——为半导体注入生命的连接
  • (备忘)Java Map 遍历
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (三十五)大数据实战——Superset可视化平台搭建
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (转)创业家杂志:UCWEB天使第一步
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET/C# 的字符串暂存池
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [C#基础]说说lock到底锁谁?
  • [corCTF 2022] CoRJail: From Null Byte Overflow To Docker Escape