简体   繁体   English

获得秒值 <table> 和第二 <td> 使用简单的HTML DOM

[英]Get value of second <table>, and second <td> using Simple HTML DOM

I'm trying get values from a shoutcast songhistory ( link ). 我正在尝试从广播的歌曲历史记录( 链接 )中获取值。

The markup of page have 2 tables like: 页面的标记有2个表格,例如:

<table>
    <tbody>
      <tr>
        <td>Header Link 1</td>
        <td>Header Link 2</td>
      </tr>
    </tbody>
</table>
<table>
    <tbody>
        <tr> <td>Lengh of song 1</td> </tr> 
        <tr> <td>Song Title 1</td> </tr> 
        <tr> <td>Lengh of song 2</td> </tr> 
        <tr> <td>Song Title 2</td> </tr> 
      </tr>
    </tbody>
</table>

I only need get the song title and save this on a database. 我只需要获取歌曲标题并将其保存在数据库中即可。

This is my code: 这是我的代码:

<?php
include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);

$host="localhost";
$username="root";
$password="";
$database="titulos";

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "No se puede conectar a la base de datos");

$html = file_get_html('http://138.36.236.207:8000/played.html');
$guardardato = "";

// Buscar 
foreach($html->find('table', 2)->find('tr', 2) as $datossc) {
    foreach($datossc->find('td') as $titulo) {

        echo $titulo->plaintext .'<br>';
        $guardardato .= $titulo->plaintext;
    }
}

$guardardato = mysql_real_escape_string($guardardato);
$query = "INSERT INTO data(name) VALUES('$guardardato')";

mysql_query($query) or die(mysql_error());

$html->clear();
unset($html);
?>

The sql process is ok, but the simple dom does work.. sql进程还可以,但是简单的dom确实可以工作。

I get that error: 我得到那个错误:

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\proyectos\\radioargenta\\oyentes\\indexprueba.php on line 19 警告:在第19行的C:\\ xampp \\ htdocs \\ proyectos \\ radioargenta \\ oyentes \\ indexprueba.php中为foreach()提供了无效的参数

在此处输入图片说明

Can help me? 可以帮我?

Thanks! 谢谢!

You have to change the for loop as follows: 您必须按如下方式更改for循环:

foreach($html->find('table', 2)->find('tr') as $datossc) {
    echo $datossc->find('td', 1)->plaintext .'<br>';
    $guardardato .= $datossc->find('td', 1)->plaintext;
}

Note that you will have Song Title in the output as well. 请注意,您还将在输出中包含“ Song Title ”。

Also, are you sure that you want to append all titles in $guardardato ? 另外,您确定要在$guardardato附加所有标题吗? This will just concatenate the titles, eg Song TitleAle Ceberio - LaCuartetera.NetAlcides - Tan bonita pero muy celosaAgrupaciOn Marylin - Agru... . 这将仅连接标题,例如Song TitleAle Ceberio - LaCuartetera.NetAlcides - Tan bonita pero muy celosaAgrupaciOn Marylin - Agru... Perhaps what you want to do is: 也许您想做的是:

$guardardato = array();

foreach($html->find('table', 2)->find('tr') as $datossc) {
    $title = $datossc->find('td', 1)->plaintext;
    if ($title != 'Song Title') {  
        echo $title .'<br>';
        $guardardato[] = $title;
    }
}

foreach($guardardato as $i) {
    $value = mysql_real_escape_string($i);
    $query = "INSERT INTO data(name) VALUES('" . $value . "')";
    mysql_query($query) or die(mysql_error());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无论如何我可以使用PHP或Simple HTML DOM获取具有特定td值的表行吗? - Is there anyway that I can get the table row with a specific td value with PHP or Simple HTML DOM? PHP简单HTML DOM-如何通过TD中的特定值查找表? - PHP Simple HTML DOM - How to find the table by a particular value in TD? simple_html_dom中的第二个file_get_contents不起作用 - second file_get_contents in simple_html_dom not working 使用PHP Simple HTML DOM获取表值 - Get table value using PHP Simple HTML DOM 如何使用PHP Simple DOM在没有属性的情况下从旁边的html table td元素中查找数据并获取文本? - How to find data and get text from html table td element next to it, without attribute using PHP Simple DOM? 如何使用php和dom获得第一,第二和第三个TD值 - how can i get first, second and third TD values using php and dom 简单的 HTML DOM 解析器。 删除表中的第一行,然后删除每个的第二列 - Simple HTML DOM Parser. Remove first row in table and then remove second column of each 通过简单的dom解析器在html表中循环并获取php中的tr,th和td - loop through html table and get tr, th and td in php with simple dom parser 如何使用表单元格值获取数据? PHP简单HTML DOM解析器 - How to get data using table cell value ? PHP Simple HTML DOM Parser simple_html_dom无法在第二级添加节点 - simple_html_dom Cannot add node in second level
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM