[英]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.