我正在为我的项目使用PHP Simple HTML DOM Parser。
我正在尝试查找特定数据并在我从包含HTML表元素内的数据的URL网站解析我的.php文件后回显它,示例如下:
<table class="example">
<tbody>
<tr>
<td>
Heading #1
<p>Description of heading #1 here ...</p>
</td>
<td>Example of data #1</td>
</tr>
<tr>
<td>
Heading #2
<p>Description of heading #2 here ...</p>
</td>
<td>Example of data #2</td>
</tr>
</tbody>
</table>
我的问题:
如何通过知道同一TR行中的第一个TD单元包含值“Heading#1 ...”从第一个TR行元素中的第二个TD单元元素获得值“数据#1的示例”一张桌子?
我已经解析了URL,现在我需要根据旁边的其他值找到值。
我应该使用一些正则表达式并为此制作一些模式吗? strpos()和数组?
答案 0 :(得分:1)
您需要为表格分区提供JavaScript的ID,以便能够获取提交的数据并将其放入带有名称和ID的隐藏输入中,以便PHP使用POST获取它们。
<script language="javascript">
function transfer_data(){
documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
documentGetElementById('ex2_hidden').value = documentGetElementById('ex2').innerHTML;
submit();
}
</script>
<table class="example">
<tbody>
<tr>
<td id="hdg1">
Heading #1
<p>Description of heading #1 here ...</p>
</td>
<td id="ex1">Example of data #1</td>
</tr>
<tr>
<td>
Heading #2
<p>Description of heading #2 here ...</p>
</td>
<td id="ex2">Example of data #2</td>
</tr>
</tbody>
</table>
在您需要使用method="post"
提交到任何地方的表单中,您需要:
<input type="hidden" name="ex1_hidden" id="ex1_hidden" />
<input type="hidden" name="ex2_hidden" id="ex2_hidden" />
<input type="button" value="Submit" onClick="transfer_data()" />
在PHP中,您可以使用$_POST['ex1_hidden']
和$_POST['ex2_hidden']
来提取它们(请记住清理提交的数据。)
这不是一种适合安全数据的方法。
您可以在标题中添加ID,并在脚本中将其设置为条件:
if(documentGetElementById('hdg1').innerHTML == "Heading #1"){
documentGetElementById('ex1_hidden').value = documentGetElementById('ex1').innerHTML;
}
您可能需要使用类似
之类的东西修剪标题上的空白 var str=documentGetElementById('hdg1').innerHTML.replace(/^\s+|\s+$/g,'');
@ {3}}
上的@Paul关于其他方式的许多有用的想法how do I strip white space when grabbing text with jQuery?
如果这是从你完全没有控制权的另一个网站上抓取的数据,但你已经在PHP变量中已经有了,你可以explode()
<td>
来计算出来数组位置包含您想要的数据。参考:How to get a table cell value using jQuery?
这就是我认为你真正想要的东西 - 可能是一个好主意,询问网站的所有者是否可以先行,但这取决于你。您使用strpos();
和数组(使用您的表格测试)走在正确的轨道上:
// only works if fopen is allowed on the site's server and in PHP5+
$handle = fopen("http://websiteyouwanttoscrape.com/file.html", "r");
$contents = stream_get_contents($handle);
$contents_array = array();
$bit_i_want = array();
// give yourself a chance
$contents = htmlspecialchars($contents);
// swap these if you don't use htmlspecialchars();
$contents_array = explode('<td>',$contents);
//$contents_array = explode('<td>',$contents);
$counter = 0;
while($counter < count($contents_array)){
if(strpos($contents_array[$counter], 'Heading #1') > 0 ){
// swap these if you don't use htmlspecialchars();
$bit_i_want = explode('</td>',$contents_array[$counter+1]);
//$bit_i_want = explode('</td>',$contents_array[$counter+1]);
echo $bit_i_want[0] . '<br />';
// uncomment break; to stop the loop if you don't
// want to look for any more instances of "Heading #1" if there were any
//break;
}
$counter++;
}
fclose($handle); //close the file