使用PHP从网页提取特定数据 [英] Extracting specific data from a web page using PHP

查看:175
本文介绍了使用PHP从网页提取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

在PHP中的HTML刮擦

我想知道是否有任何方式从网页获得一个特定的文本字符串,随时使用PHP更新。我搜遍了遍布互联网,却一无所获。只要看到preg_match可以做到这一点,但我不明白如何使用它。

I would like to know if is there any way to get from a webpage a specific string of text wich is updated every now and then using PHP. I´ve searched "all over the internet" and have found nothing. Just saw that preg_match could do it, but I didn't understand how to use it.

设想一个网页包含这个:

imagine that a webpage contains this:

<div name="changeable_text">**GET THIS TEXT**</div>

使用PHP后,如何使用 file_get_contents to把页面放在一个变量中?

How can I do it using PHP, after having used file_get_contents to put the page in a variable?

预先感谢:)

Thanks in advance :)

推荐答案

您可以使用 DOMDocument ,如下所示:

You can use DOMDocument, like this:

$html = file_get_contents( $url);

libxml_use_internal_errors( true);
$doc = new DOMDocument;
$doc->loadHTML( $html);
$xpath = new DOMXpath( $doc);

// A name attribute on a <div>???
$node = $xpath->query( '//div[@name="changeable_text"]')->item( 0);

echo $node->textContent; // This will print **GET THIS TEXT**

这篇关于使用PHP从网页提取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆