【问题标题】:Matching regular expression匹配正则表达式
【发布时间】:2019-03-30 10:22:46
【问题描述】:

我正在尝试在以下代码部分中从所有 div 中获取带有价格的数据:

<section data-v-7630ac7c="" view-more-component="">
<div data-v-5c5571e4="" table-component="" data-v-7630ac7c="">
<header data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="">Price</div> 
<div data-v-5c5571e4="" quantity="" right-align="">Quantity</div> 
<div data-v-5c5571e4="" date="">Time</div></header> <section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8611.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div> 
<div data-v-5c5571e4="" date="">13:27:17</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8612.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div> 
<div data-v-5c5571e4="" date="">13:03:12</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8517.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.04983464</div> 
<div data-v-5c5571e4="" date="">12:51:24</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8608.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.59195657</div> 
<div data-v-5c5571e4="" date="">12:32:26</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8521.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.02990000</div> 
<div data-v-5c5571e4="" date="">11:29:57</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8545.20</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.26101049</div> 
<div data-v-5c5571e4="" date="">01:20:35</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8462.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.00674370</div> 
<div data-v-5c5571e4="" date="">23:49:47</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8539.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.05799985</div> 
<div data-v-5c5571e4="" date="">23:00:53</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8446.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.02345439</div> 
<div data-v-5c5571e4="" date="">22:30:43</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8540.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.25500001</div> 
<div data-v-5c5571e4="" date="">21:55:28</div></section></div> 
<section data-v-7630ac7c="" id="my-trades"><div data-v-75ce2ffa="" data-v-7630ac7c="" button-component="" class="viewmore fullbutton">
<button data-v-75ce2ffa=""></button> 
<!----> <span data-v-75ce2ffa="" text="">View More</span> <!----></div> <!----></section> <!----> <!----></section> <!---->

我使用的正则表达式是

<div (..................) price="" (.*)">(.*)<\/div>

但我不断得到 NULL。任何帮助将不胜感激。

【问题讨论】:

  • 您使用的实际代码是什么?
  • 你想获得什么价值?请添加您的 PHP。如果 HTML 扩展到两行,. 将不会覆盖(或者可能除非您使用修饰符)。解析器会更容易使用。
  • HTML 和正则表达式不是好朋友。使用解析器,它更简单、更快且更易于维护。见:php.net/manual/en/class.domdocument.php
  • 当您足够精通时,Regex 非常适合提取此类数据。 &lt;div[^&gt;]+price=\"\"[^&gt;]+&gt;([\d.]+)&lt;。否则建议使用 DOM 遍历前端。有了这样的自动生成输出,就有可能存在适当的 API。

标签: php regex curl preg-match


【解决方案1】:

虽然强烈建议不要使用正则表达式解析 html,但您发布的 html 代码仅包含没有嵌套的 div 标签,因此您可以应用这个简单的正则表达式并完成从 div 标签获取所有价格的工作:

<div.*?price.*?>(\d+\.\d+)<\/div>

但请将此作为临时修复,如果您必须长期使用它,请使用 html 解析器。

演示,

https://regex101.com/r/JBwRM5/1

【讨论】:

    【解决方案2】:

    我都试过了,它们在这里不起作用是我正在使用的整个代码

    <?
    $curl = curl_init();
    $SearchString = "BTCCAD";
    $url = "https://einstein.exchange/trade?mode=advanced&instrument=$SearchString";
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    $result = curl_exec($curl);
    preg_match_all('<div[^>]+price=\"\"[^>]+>([\d.]+)<',$result,$matches);
    var_dump($matches);
    curl_close($curl);
    ?>
    

    【讨论】:

    • 所以我尝试了解析器,但是页面中的大部分数据都是通过 JS 创建的,所以解析器不会加载它。
    猜你喜欢
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多