【问题标题】:How to export specific A tags from a file.[html] in PHP如何从文件中导出特定的 A 标签。 [html] 在 PHP
【发布时间】:2016-11-04 21:26:42
【问题描述】:

我有一个 html 文件和一些 A 标签。

我需要从中导出一些特定的 A 标签。

这是整个html文件HTML_FILE_LINK

我只想提取dl/l/Lucy.2014.Dubbed.Audio.TinyMoviez_co(Onyx).mp3?hash=27bd643109ad5f992c28e10a33afe8dc_159484_26806_3

还有其他类似的。

我如何在 php 中做到这一点?

我试过这个:how to get a list of links in a webpage in PHP? 但没用。

如果有人能回答,我真的需要这个,我将不胜感激。

【问题讨论】:

  • 为什么这个答案不起作用?有一个完整的脚本,可以满足您的要求,还有一个链接 stackoverflow.com/q/4461105/1741542 指向另一个问题,它提供了更简单的解决方案。
  • "和其他类似的" -- 这是什么意思?你怎么知道你想要哪些标签?
  • @OlafDietsche tnx。但它会从页面中导出所有 A 标签。我只需要其中一些链接到文本“لینکمستقیمویژه”。有没有办法只导出链接到上述文本的那些?
  • @EatPeanutButter 链接到文本“لینک مستقیم ویژه”的所有 A 标签

标签: php html


【解决方案1】:

基于Parse Website for URLs 的这个答案已经给出了所有a 标签

$code = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($code);
$links = array();
foreach ($doc->getElementsByTagName('a') as $element) {
    if ($element->hasAttribute('href')) {
        $links[] = $element->getAttribute('href');
    }
}

但是你只想要特定的链接,所以你必须检查if它满足一些条件。提取所有相关部分,看看是否匹配

$text = $element->nodeValue;
$link = $element->getAttribute('href');
if ($text == "لینک مستقیم ویژه") {
    echo "$link\n";
}

把所有东西放在一起给出

$code = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($code);
foreach ($doc->getElementsByTagName('a') as $element) {
    if ($element->hasAttribute('href')) {
        $text = $element->nodeValue;
        $link = $element->getAttribute('href');
        if ($text == "لینک مستقیم ویژه") {
            echo "$link\n";
        }
    }
}

【讨论】:

  • 这正是我想要的。谢啦。由于我的声誉,我不能给 +1。
猜你喜欢
  • 1970-01-01
  • 2016-03-17
  • 2012-09-16
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多