Php 从网站获取数据
我有自己的外部网站,我想从网站上获得一些数据。我使用CURL来获取网站的内容,但是我想要的部分是: 编辑:坦率地说,我想获得facebook页面的时间戳,如果您在页面上使用Inspect元素,您将看到如下代码:Php 从网站获取数据,php,html,curl,Php,Html,Curl,我有自己的外部网站,我想从网站上获得一些数据。我使用CURL来获取网站的内容,但是我想要的部分是: 编辑:坦率地说,我想获得facebook页面的时间戳,如果您在页面上使用Inspect元素,您将看到如下代码: <span class="fsm fwn fcg"><a class="_5pcq"> <abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:01pm" data-utime="1435663827" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:02pm" data-utime="1435663828" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:03pm" data-utime="1435663829" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:04pm" data-utime="1435663830" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
</span>
您可以使用PHP对html文档的扩展,然后使用特定元素的实例。如果您已经获得html标记,您可以 试试这个:
<?php
$curl = curl_init('https://www.facebook.com/Rajnikant.Vs.CIDJokez');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
//echo $result;
/* $result =
'<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:01pm" data-utime="1435663827" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:02pm" data-utime="1435663828" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:03pm" data-utime="1435663829" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:04pm" data-utime="1435663830" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
</span>';
*/
$html = $result;
$dom = new DOMDocument();
@$dom->loadHTML($html);
$a = $dom->getElementsByTagName('abbr');
$data = array();
for ($i=0; $i < $a->length; $i++) {
$data[] = $a->item($i)->getAttribute('data-utime');
}
echo '<pre>';
print_r($data);
echo '</pre>';
我试了很多次。但这对我不起作用。就因为我是用CURL获取的吗?嗨,Jeff,你能给出完整的php吗。我可以帮你把它报废。这就是完整的PHP!!好吧,非常狂热地说,我想废弃一个facebook页面。并且想要得到第一页的时间戳。您显示的代码无效。页面是:如果您使用Inspect元素,那么您可以看到post的时间戳仍然不工作!!这对你有用吗?如果使用代码,则会显示错误“更新浏览器”
<?php
$curl = curl_init('https://www.facebook.com/Rajnikant.Vs.CIDJokez');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
//echo $result;
/* $result =
'<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:01pm" data-utime="1435663827" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:02pm" data-utime="1435663828" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:03pm" data-utime="1435663829" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:04pm" data-utime="1435663830" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
</span>';
*/
$html = $result;
$dom = new DOMDocument();
@$dom->loadHTML($html);
$a = $dom->getElementsByTagName('abbr');
$data = array();
for ($i=0; $i < $a->length; $i++) {
$data[] = $a->item($i)->getAttribute('data-utime');
}
echo '<pre>';
print_r($data);
echo '</pre>';