【问题标题】:DOM Scrape not working PHPDOM Scrape 不工作 PHP
【发布时间】:2012-06-08 00:54:11
【问题描述】:

我只是想知道为什么这对我不起作用。我想要做的是去掉 m4v 文件。我有一个类似的脚本适用于我网站上的图像,它将剥离图像,上传到目录和数据库并链接。但我不能让它以同样的方式工作。感谢您的帮助

<?php

include('simple_html_dom.php');

$html = file_get_html("http://www.mysitesvids.com/m/videos/view/36821");
$element = $html->find("file:");
$result = $element->innertext;

?>

这是来自网站的代码

<script type="text/javascript" language="javascript">
jwplayer ('embedFlashPlayer').setup         ({flashplayer:'/swf/jwplayer5.swf',id:'moviePlayer',width:602,height:404,
    file:'http://davesvideos.mysitevids.com/media/b0e9ec18eb567ce41dce906cee7e1c9f/4fcbb164/videos/m/634276.m4v',
image:'/media/80eb2eaca3c58f002be8ab5bda476e91/4fcbb164/videos/p/64/634276.jpg',
provider:'http',controlbar:'bottom',stretching:'uniform',abouttext:'mysite',aboutlink:'http://www.eroprofile.com/'});

glbUpdViews ('0','634276','0','0');
ajaxActive = false;
cmtLoad ('video', '634276', '', '');
ajaxActive = false;
cmtReply ('video', '634276', '0');


</script>

【问题讨论】:

    标签: php html parsing dom scrape


    【解决方案1】:

    使用正则表达式会更容易解决:

    preg_match( "/file:'(.+?)'/", $html, $matches );
    
    if ( $matches ) {
        echo $matches[1];
    }
    

    我假设您在页面上没有此字符串模式的其他实例。如果您这样做了,并且您只想匹配 m4v,您可以修改表达式以查找该扩展名:

    preg_match( "/file:'(.+?\.m4v)'/", $html, $matches );
    
    if ( $matches ) {
        echo $matches[1];
    }
    

    【讨论】:

      【解决方案2】:

      来自 SimpleHtmlDom 的文档,find() 仅匹配 html 元素,因此您无法使用 find() 搜索“文件:”,您可以这样做:

      $script = $html->find('script')->innertext
      

      并应用正则表达式来匹配 $script 上的 *.mv4 文件。

      或者,您可以将正则表达式匹配直接应用于文件内容。

      【讨论】:

      • mysitesvids.com/m/videos/view/36821"); $script = $html->find('script')->innertext ?>
      • $script 将包含&lt;script&gt;...&lt;/script&gt; 中的完整文本,您必须添加一些正则表达式匹配才能获得所需的url 检查preg_match
      猜你喜欢
      • 2020-11-21
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 2022-11-23
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多