c - 使用 cURL 和 C API 通过 http 提取动态数据的值

标签 c http curl libcurl data-extraction

我正在尝试提取网络中 Wifi 接入点的 RSSI 值。唯一可用的解决方案是使用其 IP 地址连接它并尝试读取其状态页面中的值。 在我的研究过程中,我找到了 cURL 库,并编写了一些代码来连接和提取状态页面。但我只有页面的代码,没有我需要获取的值。

因此,我需要您的帮助来找到提取数据值(value)的正确方法。

我的配置:目前是Linux Ubuntu,但SW将在类似树莓派的界面、Eclipse CDT中使用。

我写的代码:

CURL *curl;
CURLcode res;

curl = curl_easy_init();
if (curl)
{
    curl_easy_setopt(curl, CURLOPT_USERPWD, "admin:password");
    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.5.179");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);

}

我需要读取的数据(wlan1_signal):

Signal Strength:</td>

                                            <td >

                                                <div class="style2">

                                                    &nbsp;<span id="wlan1_signal">None</span>&nbsp;</div>

                                            </td>

感谢您的帮助。

编辑: 在页面的html代码上,我发现执行了一个脚本来生成动态值:

<script language="javascript" src="js/status.js" type="text/javascript"></script>

在此脚本 ( http://192.168.5.179/en/js/status.js ) 中,我找到了生成 RSSI 值的代码行:

remote_call("get_wifi_info", wlan_devs[index].cdev, 
                function(data){ ...
$("#wlan"+index+"_signal").html(get_regex(data, /Link Quality=(\S+)/));
...

但我的问题仍然相同:如何读取生成的值?

最佳答案

如果您只需要检索值,而不考虑性能,则可以使用 bash 命令。

curl -s -u admin:password "http://192.168.5.179" | grep -Po '(?<=<span id="wlan1_signal">)[^<]+'

关于c - 使用 cURL 和 C API 通过 http 提取动态数据的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49139329/

相关文章:

c - 如何按 '&' 将字符串拆分为标记

c - 如何在 C 函数中使用 __VA_ARGS__ 而不是宏?

file - http页面未完全下载QT

php - 发送 SOAP 1.2

c - MPI Reduce 和 Broadcast 工作,但会导致 future 的返回值失败

C 提供带二进制位的 float 会产生错误的数字

c# - 如何在 GET 请求中包含内容?

http - 如何检查 HTTP 服务器保持事件超时?

javascript - YouTube API : How to upload thumbnail (JS)

PHP MySQL更新和奇怪的字符?