Php 避免误导并继续使用html\u simple\u dom
当我这样做时:Php 避免误导并继续使用html\u simple\u dom,php,html,api,Php,Html,Api,当我这样做时: if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href) { echo "infobox"; infobox(); } 而链接没有找到,我得到这个消息; 致命错误:对非对象调用成员函数children() 我希望出现错误,但我希望继续运行代码 谢谢 仍然有相同的错误,我附加代码:
if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href) {
echo "infobox";
infobox();
}
而链接没有找到,我得到这个消息;
致命错误:对非对象调用成员函数children()
我希望出现错误,但我希望继续运行代码
谢谢
仍然有相同的错误,我附加代码:
try {
if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href) {
echo "infobox";
infobox();
} else if (is_Object($html->find(".thumbinner", 0)) === true && is_Object($html->find(".thumbinner", 0)->children(0)) === true) {
echo "thumbinner";
thumbinner();
} else if (is_Object($html->find(".ambox-image", 0))) {
echo "ambos";
} else {
echo "errors<br />";
$errors++;
$nomPersonatge[] = $nomDifunt;
}
}catch(Exception $e) {
}
echo "Fi ". $i. " part<br />";
试试看{
如果($html->find(“.infobox_v2”,0)->子对象(1)->子对象(0)->子对象(0)->href){
回声“信息盒”;
infobox();
}否则,如果(is_对象($html->find(.thumbiner),0))==true&&is_对象($html->find(.thumbiner,0)->子对象(0))==true){
呼应“拇指内”;
拇指内侧();
}else if(是_对象($html->find(.ambox image,0))){
回音“ambos”;
}否则{
回显“错误
”;
$errors++;
$nomPersonatge[]=$nomDifunt;
}
}捕获(例外$e){
}
回声“Fi”$我“部分
”;
谢谢你的回答,谢谢你的不切实际
以下是完整代码的示例:
<?php
include "../admin/include/configuracion.php";
// include "../admin/include/database.php";
include "../admin/include/funciones.php";
include "../admin/include/tablas.php";
include('simple_html_dom.php');
?>
<!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>Documento sin título</title>
<script type="text/javascript">
</script>
</head>
<body>
<?php
mysql_conexion();
$isObject = false;
$errors = 0;
$nomPersonatge = array();
$nomDifunt = "";
$query = "select nom from dpsinfo limit 10";
$context = stream_context_create();
stream_context_set_params($context, array('user_agent' => 'UserAgent/1.0'));
$id_consulta = @mysql_query($query);
for($i=0; $i<@mysql_num_rows($id_consulta); $i++) {
//Bloc per agafar el nom del difunt i cambiar espais per "_"
$nomDifunt = @mysql_result($id_consulta, $i, 0);
$nomDifunt = str_replace (" ", "_", $nomDifunt);
//Montem la url i la carreguem a la variable $html
$url="http://es.wikipedia.org/wiki/".$nomDifunt;
echo $url." <br />";
$html = file_get_html($url, 0, $context);
if(is_Object($html)) {
//Monto el link de la pagina de dades del personatge
$url2 = "http://commons.wikimedia.org";
try {
if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href && $html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)) {
echo "infobox";
infobox();
} else if (is_Object($html->find(".thumbinner", 0)) === true && is_Object($html->find(".thumbinner", 0)->children(0)) === true) {
echo "thumbinner";
thumbinner();
} else if (is_Object($html->find(".ambox-image", 0))) {
echo "ambos";
} else {
echo "errors<br />";
$errors++;
$nomPersonatge[] = $nomDifunt;
}
} catch(Exception $e) {
$e->getMessage();
}
echo "Fi ". $i. " part<br />";
} else {
echo "<b>errors else</b><br />";
$errors++;
$nomPersonatge[] = $nomDifunt;
echo "Fi ". $i. " part<br />";
}
} //Tanca el primer for
function infobox() {
global $html, $url2;
$url2 .= $html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href;
$urlfinal = str_replace ( "Archivo:" ,"File:" , $url2); //Substitueixo Archivo per File perquè si no, no trobarà res
$html2 = file_get_html($urlfinal); //Extraic tot el contingut de la nova pàgina generada
$fotogran = $html2->find(".fullImageLink", 0)->children(0)->children(0)->src;
echo $fotogran."<br />";
echo $urlfinal."<br />";
}
function thumbinner() {
global $html, $url2;
$url2 .= $html->find("#mw-content-text", 0)->children(0)->children(0)->children(0)->href;
$urlfinal = str_replace ( "Archivo:", "File:", $url2);
$html2 = file_get_html($urlfinal);
$fotogran = $html2->find(".fullImageLink", 0)->children(0)->children(0)->src;
echo $fotogran."<br />";
echo $urlfinal."<br />";
}
?>
</body>
</html>
辛蒂图罗文件
实际上,这个脚本让我的网页崩溃了
谢谢你的关注。如果您的主要概念是从维基百科获取所有图像,请使用以下代码(或维基百科API):
<?php
include('simple_html_dom.php');
set_time_limit(0);
function file_get_safe($link) {
$content = stream_context_create(
array(
"http" => array(
"timeout" => 30,
"header" =>
implode("\r\n",
array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
"Accept-Language: en-US,en;q=0.8"
)
)
)
));
return @file_get_contents($link, FALSE, $content);
}
function wiki_big_image($image_link) {
$output = NULL;
if (filter_var($image_link, FILTER_VALIDATE_URL) !== FALSE) {
$data = @str_get_html(file_get_safe($image_link));
if (is_object($data)) {
$big_image = $data->find("#file a[href*='upload']", 0)->href;
$big_image = "http:{$big_image}";
if (filter_var($big_image, FILTER_VALIDATE_URL))
$output = $big_image;
}
}
return $output;
}
function grab_wiki_images($wiki_word, $big_image = FALSE) {
$output = array();
$big_image = $big_image === TRUE;
if (is_string($wiki_word) && strlen($wiki_word)) {
$wiki_word = str_replace(" ", "_", $wiki_word);
$link = "http://en.wikipedia.org/wiki/{$wiki_word}";
$data = @str_get_html(file_get_contents("wiki.dat"));
if (is_object($data)) {
foreach ($data->find("a[href*='\\/wiki\\/File:']") as $elm) {
$small_image = "http://en.wikipedia.org{$elm->href}";
if (@preg_match("/\\.(jpeg|jpg|png|svg)$/i", $small_image))
$output[] = $big_image ? wiki_big_image($small_image) : $small_image;
unset($elm);
}
}
}
return $output;
}
$images = grab_wiki_images("Michael Jackson");
print_r($images);
?>
仍然不起作用,我用新的回复编辑了答案。感谢您的回答。“如果您的主要概念是从维基百科获取所有图像,请使用”…它们的effing API。提供HTML代码。您想解析什么。我可以帮你……:)我在主要问题中向您提供全部评论,谢谢您的关注。我正在解析维基百科上关于已故名人的网页。再次感谢!有人能帮我吗(尝试此步骤:打开simple_html_dom.php,找到这一行define('MAX_FILE_SIZE',600000)
并替换为define('MAX_FILE_SIZE',2*1024*1024);
这里我设置了2MB的最大文件大小。如果不起作用,试试看这里问…)我已经为你更新了我的答案。请查看我的答案。:)