如何编写 preg_match_all 仅用于抓取一个特定元素?

发布于 2024-09-25 13:36:30 字数 861 浏览 3 评论 0 原文

在该网站允许我访问他的 API 之前,我只需要显示该网站的 2 件事:

我想抓住什么 // 实时页面上的示例

这两个内容包含在 div 中:

<div style="float: right; margin: 10px;">
here what i want to display on my website
</div>

问题是我在stackoverflow上找到了一个例子,但我以前从未写过preg_match。 如何处理我想要抓取的数据?谢谢

<?php   $html = file_get_contents($st_player_cv->getUrlEsl());

preg_match_all(
    'What do i need to write here ?',
    $html,
    $posts, // will contain the data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $premium = $post[1];
    $level = $post[2];

    // do something with data
}

Until the website give me an access to his API, i need to display only 2 things from this website :

What i want to grab
// Example on a live page

Those 2 things are contained in a div :

<div style="float: right; margin: 10px;">
here what i want to display on my website
</div>

The problem is that i found an example on stackoverflow, but i never wrote preg_match before. How to do this with the data i want to grabb ? Thank you

<?php   $html = file_get_contents($st_player_cv->getUrlEsl());

preg_match_all(
    'What do i need to write here ?',
    $html,
    $posts, // will contain the data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $premium = $post[1];
    $level = $post[2];

    // do something with data
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶花眉 2024-10-02 13:36:30

执行此操作的 DOM 方法是

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->loadHTMLFile('http://www.esl.eu/fr/player/5178309/');
libxml_clear_errors();

$xPath = new DOMXPath($dom);
$nodes = $xPath->query('//div[@style="float: right; margin: 10px;"]');
foreach($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}

但是 页面中存在大量 JavaScript,它们会在页面加载后大量修改 DOM。由于任何基于 PHP 脚本的获取都不会执行任何 JavaScript,因此我们在 XPath 中搜索的样式尚不存在,我们不会得到任何结果(Hannes 建议的正则表达式出于同样的原因不起作用)。徽章上的等级数字也还不存在。

正如 Wrikken 在评论中指出的那样,似乎还有某种机制可以阻止某些请求。我曾经收到过该消息,但我不确定是什么触发了它,因为我也可以多次获取页面。

长话短说:您无法通过此页面实现您想要做的事情。

The DOM way to do it would be

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->loadHTMLFile('http://www.esl.eu/fr/player/5178309/');
libxml_clear_errors();

$xPath = new DOMXPath($dom);
$nodes = $xPath->query('//div[@style="float: right; margin: 10px;"]');
foreach($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}

but there is a whole slew of JavaScript in the page that modifies the DOM heavily after the page was loaded. Since any PHP script based fetching will not execute any JavaScript, the style we search for in the XPath does not exist yet and we won't get any results (the Regex suggesed by Hannes doesn't work for the same reason). Neither do the level numbers on the badge exist yet.

As Wrikken pointed out in the comments, there also seems to be some mechanism to block certain requests. I had the message once, but I am not sure what triggers it, because I could also fetch page on several occasions.

To cut a long story short: you cannot achieve what you are trying to do with this page.

逆蝶 2024-10-02 13:36:30

如果你想要更通用的

  preg_match('/<div[^>]+?>(.*?)<\/div>/', $myhtml, $result);
  echo $result[1] . "\n";

$myhtml 包含你必须分析的代码 html。 $result 是包含正则表达式应用后的 regexp 和 () 内容的数组。 $result[1] 将为您提供

之间的内容。

这样,即使 不同(类名更改或属性不同),它仍然可以工作。

If you want something more generic

  preg_match('/<div[^>]+?>(.*?)<\/div>/', $myhtml, $result);
  echo $result[1] . "\n";

$myhtml contains the code html you have to analyze. $result is the array that contains the regexp and () content after the regular expression was applied. $result[1] will give you what is between the <div ... > and </div>.

This way, even if the <div differs (class name change or different attributes), it'll still work.

蓬勃野心 2024-10-02 13:36:30

这个正则表达式 '#

(.*)

#' 应该可以解决问题(是的),但我建议你使用 DOM & X 路径。

编辑:

这是一个 Xpath / DOM 示例:

$html = <<<HTML
<html>
<body>
    <em>nonsense</em>
    <div style="float: right; margin: 10px;"> here what i want to display on my website </div>
    <div> even more nonsense </div>
</body>
</html>

HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('//div[@style="float: right; margin: 10px;"]');
echo $elements->item(0)->nodeValue;

this regex '#<div style="float: right; margin: 10px;">(.*)</div>#' should do the trick (yeah) but i would advice you to use DOM & XPath.

edit:

Here is an Xpath / DOM Example:

$html = <<<HTML
<html>
<body>
    <em>nonsense</em>
    <div style="float: right; margin: 10px;"> here what i want to display on my website </div>
    <div> even more nonsense </div>
</body>
</html>

HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('//div[@style="float: right; margin: 10px;"]');
echo $elements->item(0)->nodeValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文