【问题标题】:How to pull most recent gas price from this example page [closed]如何从此示例页面中提取最新的汽油价格 [关闭]
【发布时间】:2013-09-28 21:31:26
【问题描述】:

我正在尝试使用 php 从this website 以设定的时间间隔(比如每天)提取最新的汽油价格。然后我想对其应用我自己的格式。

我该怎么办?

我试图阅读简单的 HTML DOM,但我对编程有点陌生,所以我有点困惑。 如果有人可以指导我,我将不胜感激。

【问题讨论】:

  • 我刚刚查看了链接,价格非常巧妙地隐藏在 css 中。
  • “聪明”有点言过其实。提取信息意味着只需获取所有.sp_p [class^=p]s 并注意类名的第二个字符(将匹配[0-9.])。
  • @AuntJamaima 没有冒犯,但这是隐藏数据的愚蠢方式。因为它真的没有帮助或隐藏。任何可以破解几个 php 函数的人都可以轻松提取该数据。
  • 在你们中的任何一个人发表评论之前,我在下面的答案中提取了价格表。我不知道他们以这种方式掩盖价格的目的是什么,除了可能让某人多花 10 分钟来提取数据。

标签: php html dom curl


【解决方案1】:

以下内容应该可以帮助您度过最难的部分。它从巧妙掩盖的 css 中撕下所有价格。您需要使用另外两个正则表达式来获取加油站名称和地址。

<?php
$page = file_get_contents("http://www.atlantagasprices.com/index.aspx?area=Decatur&area=North%20Decatur");
preg_match_all('/<div class=\"p\d/',$page,$pricesRaw);

foreach($pricesRaw[0] as $key => $value)
{
    $priceDigits[$key] = str_replace('<div class="p','',$value);
}

$x=0;
$prices = array();
while($x<count($priceDigits))
{
    array_push($prices, $priceDigits[$x].".".$priceDigits[$x+1].$priceDigits[$x+2]);
    $x=$x+3;
}

var_dump($prices); //this only shows that the prices array now holds all the gas prices listed on the page.

?>

【讨论】:

    【解决方案2】:

    您是要我们为您编写代码吗?

    如果不是你这样做:

    1. 使用php curl抓取页面
    2. 使用http://www.php.net/manual/en/class.domdocument.php 抓取内容
    3. 将您的 php 作为每日 cron 运行

    【讨论】:

    • 非常感谢所有回复!我不想编写整个代码...我只是想指出我应该用来解决问题的工具的正确方向。
    猜你喜欢
    • 2017-04-20
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多