Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php simplehtmldom解析脚本,用于断开以纯文本形式出现的数据_Php_Html_Web Scraping_Simple Html Dom - Fatal编程技术网

Php simplehtmldom解析脚本,用于断开以纯文本形式出现的数据

Php simplehtmldom解析脚本,用于断开以纯文本形式出现的数据,php,html,web-scraping,simple-html-dom,Php,Html,Web Scraping,Simple Html Dom,这是我的脚本,我在其中获取三个项目药物名称、通用名称和类名。我这里的问题是,我成功地分别获取了药品名称,但是通用名称和类名是字符串。如果您运行该脚本,您将更好地了解我实际上想说的内容,我想在表中存储泛型名称和类名称 脚本 <?php error_reporting(0); //simple html dom file require('simple_html_dom.php'); //target url $html = file_get_html('http://www.drugs

这是我的脚本,我在其中获取三个项目药物名称、通用名称和类名。我这里的问题是,我成功地分别获取了药品名称,但是通用名称和类名是字符串。如果您运行该脚本,您将更好地了解我实际上想说的内容,我想在表中存储泛型名称和类名称

脚本

<?php

error_reporting(0);

//simple html dom file
require('simple_html_dom.php');

//target url
$html = file_get_html('http://www.drugs.com/condition/atrial-flutter.html?rest=1');

//crawl td columns

 foreach($html->find('td') as $element)
{   
    //get drug name
    $drug_name = $element->find('b');
    foreach($drug_name as $drug_name)
    {
        echo "Drug Name:-".$drug_name;

        foreach($element->find('span[class=small] a',2) as $t)
        {
            //get the inner HTML
            $data = $t->plaintext;
            echo $data;
        }

        echo "<br/>";
    }
}

?>


提前感谢

您当前的代码与您需要执行的有点远,但是您可以利用css选择器使这些元素变得更容易

例如:

$data = array();
$html = file_get_html('http://www.drugs.com/condition/atrial-flutter.html?rest=1');
foreach($html->find('tr td[1]') as $td) { // you do not need to loop each td!
// target the first td of the row
    $drug_name = $td->find('a b', 0)->innertext; // get the drug name bold tag inside anchor
    $other_info = $td->find('span.small[2]', 0); // get the other info
    $generic_name = $other_info->find('a[1]', 0)->innertext; // get the first anchor, generic name
    $children_count = count($other_info->children()); // count all of the children
    $classes = array();
    for($i = 1; $i < $children_count; $i++) { // since you already got the first, (in position zero) iterate all children starting from 1
        $classes[] = $other_info->find('a', $i)->innertext; // push it inside another container
    }

    $data[] = array(
        'drug_name' => $drug_name,
        'generic_name' => $generic_name,
        'classes' => $classes,
    );
}

echo '<pre>';
print_r($data);
$data=array();
$html=file\u get\u html('http://www.drugs.com/condition/atrial-flutter.html?rest=1');
foreach($html->find($trtd[1]”)作为$td){//您不需要循环每个td!
//以行的第一个td为目标
$druge\u name=$td->find('a b',0)->innertext;//在锚内获取药物名称粗体标记
$other_info=$td->find('span.small[2]',0);//获取其他信息
$generic\u name=$other\u info->find('a[1]”,0)->innertext;//获取第一个锚点,generic name
$children\u count=count($other\u info->children());//计算所有子项
$classes=array();
对于($i=1;$i<$children_count;$i++){//,因为您已经获得了第一个(在零位)迭代从1开始的所有子项
$classes[]=$other_info->find('a',$i)->innertext;//将其推送到另一个容器中
}
$data[]=数组(
'drug\u name'=>$drug\u name,
“通用名称”=>$generic\u name,
“类”=>$classes,
);
}
回声';
打印(数据);

@user2960749我很高兴helped@user2960749:如果建议的解决方案对您有所帮助,您应该接受它。只需单击帖子分数下方的小勾号。