首页 > 解决方案 > I can not get content from specific url

问题描述

i have one php file and i want to scrape specific element from the url below, but i have problem in my first step. you can see my code below and you can run it to see the content is wrong:

<?php
$ch = curl_init();
$url ="https://www.weather-forecast.com/locations/Rasht/forecasts/latest";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html_content = curl_exec($ch);
curl_close($ch);
echo $html_content;
?>

i have this code and when i opened that url with my chrome browser , it's working. but when i want to get content from my php file , wrong page has been get.

标签: phpcurl

解决方案


http://php.net/manual/en/book.curl.php,有很多选项可以与 curl 一起使用,稍微玩一下下面的代码就可以与您的 url 一起使用。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,"https://www.weather-forecast.com/");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = curl_exec($ch);
curl_close($ch);

print $data;

并且$data变量具有整个页面的 html。

您可以尝试使用 php DOM 方法解析页面中的数据,并将它们转换为您想要的数据类型(对象类、数组等)。


推荐阅读