如何使用PHP抓取单个linkedin帖子

问题描述 投票:0回答:1

我正在尝试使用PHP从linkedin中抓取帖子。

考虑此URL:https://www.linkedin.com/posts/linkedin_simple-reminder-believe-in-yourself-activity-6668872904807092224-pMVk

我正在尝试获取此帖子的内容(这是用户编写的常规供稿帖子)。意思是我应该得到这样的结果:“简单的提醒:相信自己。”

有人这样做吗?我试图用file_get_contents()提取信息,然后执行$ xpath-> query(“ // * [@ id ='ember86']”);以及其他方法,但我要么为null要么[无法检索全文内容]

有关如何执行此操作的任何见解?

php web-scraping
1个回答
2
投票

您最好的选择是使用正则表达式提取所需内容。例如:

<?php 
$page=@file_get_contents("https://www.linkedin.com/posts/linkedin_simple-reminder-believe-in-yourself-activity-6668872904807092224-pMVk");
if($page){
    preg_match_all("/<p class=\"share-update-card__update-text public-post__update-text\">([^<]+)<\/p>/",$page,$matches);
    if(isset($matches[1][0])){
        echo $matches[1][0];
    }else{
        echo "No match found!";
    }
}else{
    echo "failed to load page";
}
?>

更新:

<?php 
$page=@file_get_contents("https://www.linkedin.com/posts/dineshkarna_the-biology-of-courage-what-is-that-ugcPost-6668335979088216064-nQwk/");
if($page){
    $data=array();

    preg_match_all("/<p class=\"share-update-card__update-text public-post__update-text\">([^<]+)<\/p>/",$page,$title_matches);
    if(isset($title_matches[1][0])){
        $data["title"]=$title_matches[1][0];
    }else{
        $data["title"]=null;
    }

    preg_match_all("/<video class=\"share-native-video__node video-js\"data-sources=\"(\[[^\]]*\])\"data-poster-url=\"([^\"]*)\".*><\/video>/",$page,$video_matches);
    if(isset($video_matches[1][0])){
        $data["videos"]=json_decode(html_entity_decode($video_matches[1][0]),true);
        var_dump($data["videos"]); 
        exit;
    }else{
        $data["videos"]=null;
    }

    var_dump($data);
    exit;
}else{
    echo "failed to load page";
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.