php - 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.
解决方案
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 方法解析页面中的数据,并将它们转换为您想要的数据类型(对象类、数组等)。
推荐阅读
- c# - Automapper Aftermap“找不到方法”异常
- php - 有没有办法使用 Html、php 等获取当前的 Windows 登录用户名?
- c++ - 这是什么 `(*(T*)nullptr)` 老把戏?
- javascript - 如何折叠响应式导航栏
- python - 使用 strftime 更改为字符串时如何将 datetime.date 更改为 st,nd,rd,th
- awkward-array - Uproot 4中的TLorentz向量
- r - 在 rmd 子文件中嵌套不同的块
- mysql - Docker 内外不同的 MySQL 字符集
- vb.net - 应用 。启动路径不起作用 vb.net
- javascript - 在 Jest 中,如何测试组件中的非模拟方法是否在单击时运行?