目前我正在使用simple_html_dom来爬取一个网站,点击此处查看我正在抓取的网站,所有内容都可以正常返回,除了它会为每个帖子持续放置相同的内容... 点击此处查看演示
$page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : '';
$html = file_get_html('http://screenrant.com/movie-news/'.$page);
foreach($html->find('#site-top > div.site-wrapper > div.top-content > article > section > ul > li > div.info > h2 > a') as $element)
{
print '<br><br>';
echo $url = ''.$element->href;
$html2 = file_get_html($url);
$image = $html2->find('meta[property=og:image]',0);
$news['image'] = $image->content;
#print '<br><br>';
// Ending The Featured Image
#site-top > div.site-wrapper > div.top-content > article > section > ul > li:nth-child(2)
$title = $html2->find('#site-top > div.site-wrapper > div.top-content > article > header.single-header > h1',0);
$news['title'] = $title->plaintext;
// Ending the titles
print '<br>';
#site-top > div.site-wrapper > div.top-content > article > div
$articles = $html2->find('#site-top > div.site-wrapper > div.top-content > article > div > p');
foreach ($articles as $article) {
#echo "$article->plaintext<p>";
$news['content'] = $news['content'] . $article->plaintext . "<p>";
}
print '<pre>';print_r($news);print '</pre>';
print '<br><br>';
// mysqli_query($DB,"INSERT INTO `wp_scraped_news` SET
// `hash` = '".$news['title']."',
// `title` = '".$news['title']."',
// `image` = '".$news['image']."',
// `content` = '".$news['content']."'");
// print '<pre>';print_r($news);print '</pre>';
}
我不知道我在这里做错了什么,但我认为有两件事情可能出了问题,并且我已经尝试过这两件事情,但没有成功。
1. 我在布置我的foreach时做错了一些事情。
2. 网站对于每篇新文章都在更改选择器。
在这两种情况下,我可能都错了...但是我已经尝试了两个小时,现在已经放弃了...非常感谢任何帮助。
#site-top > div.site-wrapper > div.top-content > article > section > ul > li:nth-child(2) > div.info > div > div > p
-- 可能需要将li:nth-child(2)
更改为li
.. 这是一个开始的地方。 - Clay