使用 PHP 从网页获取脚本内容


Get script content from web page using PHP

我想从网页的script标签中获取特定内容。
这是 HTML 页面。

<html>
    <head>
        <title></title>
       <script>
         //some other srcipt code
       </script>
    </head>
<body>
       <script>
         //some other srcipt code
       </script>
    <!-- some other html code -->
<script>
var abc = {"asld":"asdjk","thisone":"https:'/'/www.example.com'/$N.jpg|#1000#M$M#JSuL8|1000#M$M#fW4EC0jcOl8kUY_QBZbOASsF8t0","user_gender":"m"};
</script>
</body>
</html>   

我只想获取这个值fW4EC0jcOl8kUY_QBZbOASsF8t0该值位于脚本变量 var abcthisone 的最后一个属性中。
我可以用PHP HTML DOM做到这一点吗.如果没有,那我该怎么办

您可以使用正则表达式轻松获取它

"thisone":".*#(.*)",

假设网页 http://example.com,

$data = file_get_contents('http://example.com');
preg_match_all("/'"thisone'":'".*#(.*)'",/", $data, $output);
echo $output[1][0]; // prints fW4EC0jcOl8kUY_QBZbOASsF8t0

希望对您有所帮助!