Php xpath障碍 [英] Php xpath impediment
问题描述
我有这段代码:
require_once(< span class =code-string> Xpath.php);
// ini_set('xdebug.max_nesting_level',200);
scrape ( http://filmehd.net/page/1);
set_time_limit(0);
函数scrape($ url)
{
$ xpath = new XPATH($ url);
$ get_image_src = $ xpath-> query(' // H 2 /一个/ IMG / @ src'中跨度>);
$ get_image_movie_src = $ xpath-> query(' // H2 [2] / A / @ href属性跨度>);
$ get_image_movie_anchor = $ xpath-> query(' // H 2 /一个/ @标题'跨度>);
$ next_page = $ xpath-> query(' // DIV / A [10] / @ href属性跨度>);
$ data = array();
echo < pre>< /跨度>;
for($ x = 0; $ x< $ get_image_movie_anchor-> length; $ x ++)
{
// < span class =code-comment> $ data [$ x] ['image_src'] = $ get_image_src-> item($ x) - > nodeValue;
$ data [$ x] [' image_movie_src'] = $ get_image_movie_src-> item($ x) - > nodeValue;
// $ data [$ x] ['image_movie_anchor'] = $ get_image_movie_anchor-> item($ x) - > nodeValue;
echo $ data [$ x] [' image_movie_src'];
// echo'< div>< a href ='。$ data [$ x] ['image_movie_src']。'>< img src ='。$ data [$ x] ['image_src']。'/>'。$ data [$ x] ['image_movie_anchor']。' < / a>< / div>';
}
if($ next_page-> length> 0)
{
刮($ next_page->项(0) - >的nodeValue);
}
}
当我使用此代码运行页面时我得到:
Quote:注意:尝试在C:\ wamp \ www \\\ teraper \filmeHdScraper中获取非对象的属性。第20行的php
如果我修改:
Quote:// h2 [2] / a / @ href
to
引用:// h2 / a / @ href
错误消失,问题是如果我使用不会产生错误的xpath我将获得重复的内容,因为在该网站上有两个h2标签。我该怎么办 ?怎么能让
引用:// h2 [2] / a / @ href
work?
url)
{
xpath = new XPATH(
url);
Hello !
I have this piece of code :
require_once("Xpath.php");
//ini_set('xdebug.max_nesting_level', 200);
scrape("http://filmehd.net/page/1");
set_time_limit(0);
function scrape ($url)
{
$xpath = new XPATH($url);
$get_image_src = $xpath->query('//h2/a/img/@src');
$get_image_movie_src = $xpath->query('//h2[2]/a/@href');
$get_image_movie_anchor = $xpath->query('//h2/a/@title');
$next_page = $xpath->query('//div/a[10]/@href');
$data = array();
echo"<pre>";
for($x =0 ; $x<$get_image_movie_anchor->length; $x++)
{
// $data[$x]['image_src'] = $get_image_src->item($x)->nodeValue;
$data[$x]['image_movie_src'] = $get_image_movie_src->item($x)->nodeValue;
// $data[$x]['image_movie_anchor'] = $get_image_movie_anchor->item($x)->nodeValue;
echo $data[$x]['image_movie_src'] ;
//echo '<div><a href="'.$data[$x]['image_movie_src'].'"><img src="'.$data[$x]['image_src'].'"/>'.$data[$x]['image_movie_anchor'].'</a></div>';
}
if($next_page->length > 0)
{
scrape($next_page->item(0)->nodeValue);
}
}
when i run the page using this code i get :
Quote:Notice: Trying to get property of non-object in C:\wamp\www\scraper\filmeHdScraper.php on line 20
if i modify :
Quote://h2[2]/a/@href
to
Quote://h2/a/@href
the error goes away ,the problem is that if i use the xpath that doesn't generate the error i will get duplicate content because on that website there are two h2 tags . What do i do ? how can i make
Quote://h2[2]/a/@href
work ?
url) {
xpath = new XPATH(
url);
这篇关于Php xpath障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!