我正在使用简单的 HTML DOM 抓取一些网站的链接,但是我遇到了许多网站使用相对链接而不是完整 URL 的问题。
所以发生的事情是我抓取链接,并将它们直接输出到我的网站上,但每个链接都指向www.mydomain.com/somearticle而不是www.crawleddomain.com/somearticle。
我做了一些挖掘,发现了BASE 标签。由于我从多个站点爬取,我不能只为我的网站设置一个基本标记,因为它会从输出变为输出。因此,我正在寻找仅针对某个 div 的基本标签。我偶然发现了这个答案。
但是,我尝试手动包含下面的基本 url,但这不起作用:
echo ('http://www.baselink.com/' . strip_tags($post, '<p><a>'));
我还尝试了第二个选项,该correct_urls($html, $baseurl);
功能,但显然不存在。
有没有办法在 PHP 的 for 循环中将基本 URL(或附加)更改为相对 URL?
这是我正在使用的代码:
<div class='rcorners1'>
<?php
include_once('simple_html_dom.php');
$target_url = "http://www.buzzfeed.com/trending?country=en-us";
$html = new simple_html_dom();
$html->load_file($target_url);
$posts = $html->find('ul[class=list--numbered trending-posts trending-posts-now]');
$limit = 10;
$limit = count($posts) < $limit ? count($posts) : $limit;
for($i=0; $i < $limit; $i++){
$post = $posts[$i];
$post->find('div[class=trending-post-text]',0)->outertext = "";
echo strip_tags ($post, '<p><a>');
}
?>
</div>
</div>