gpt4 book ai didi

php - 使用简单的 html dom 获取链接

转载 作者:可可西里 更新时间:2023-11-01 00:58:46 25 4
gpt4 key购买 nike

我正在尝试从站点获取链接。

"http://www.perfumesclub.com/es/perfume/mujer/c/ "

为此,在简单的 html Sun 中使用“user-agent”。

但是我得到这个错误..

Fatal error: Call to a member function find() on string in C:\Users\Desktop\www\funciones.php on line 448

这是我的代码:

谢谢^^

$url = 'http://www.perfumesclub.com/es/perfume/mujer/c/';

$option = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
)
);
$context = stream_context_create($option);
$html = new simple_html_dom();
$html = file_get_contents ($url, false, $context);


$perfumes = $html->find('.imageProductDouble'); --> this is line 448

foreach($perfumes as $perfume) {
// Get the link

$enlaces = "http://www.perfumesclub.com" . $perfume->href;
echo $enlaces . "<br/>";
}

最佳答案

将 file_get_contents 包装在 str_get_html 函数中

// method 1
$html = new simple_html_dom();
$html->load( file_get_contents ($url, false, $context) );
// or method 2
$html = str_get_html( file_get_contents ($url, false, $context) );

你正在创建一个新的 dom 并将其分配给变量 $html,而不是读取返回字符串的 url 并将其设置为 $html,从而覆盖你的 simple_html_dom 实例,因此当你调用 find 方法时你有一个字符串而不是对象。

关于php - 使用简单的 html dom 获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com