【发布时间】:2014-03-08 03:17:06
【问题描述】:
我尝试使用简单的 Dom Parser 显示 Wikipedia 信息框的内容,但这给我带来了问题。这是代码。`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//The folder where you uploaded simple_html_dom.php
require_once('simple_html_dom.php');
//Wikipedia page to parse
$html = file_get_html('https://en.wikipedia.org/wiki/Burger_King');
foreach ( $html->find ( 'table[class=infobox vcard]' ) as $element ) {
$cells = $element->find('td');
$i = 0;
foreach($cells as $cell) {
$left[$i] = $cell->plaintext;
if (!(empty($left[$i]))) {
$i = $i + 1;
}
}
$cells = $element->find('th');
$i = 0;
foreach($cells as $cell) {
$right[$i] = $cell->plaintext;
if (!(empty($right[$i]))) {
$i = $i + 1;
}
}
print_r ($right);
echo "<br><br><br>";
print_r ($left);
//If you want to know what kind of industry burger king is
//echo "Burger king is $right[2], $left[2]
}
?>
</body>
</html>
该代码不适用于https://en.wikipedia.org/wiki/United_Kingdom 等任何其他页面,它只能使用https://en.wikipedia.org/wiki/Burger_King。 这是错误消息我收到致命错误:在第 16 行调用 C:\wamp\www\MyApps\Inbox.php 中非对象上的成员函数 find()
【问题讨论】:
-
simple_html_dom.php你的 php 文件里有吗? -
是在我的本地文件夹中
-
HTTPS 包装器是否启用?
-
不!但我现在找到了如何使用stackoverflow.com/questions/2305954/… 启用它。
-
谢谢。它可以工作,但只有en.wikipedia.org/wiki/Burger_King。知道为什么它不适用于其他页面,例如 en.wikipedia.org/wiki/India
标签: php