从外部网页(php 或 java)获取值

发布于 2024-10-10 02:15:57 字数 141 浏览 0 评论 0原文

我需要从外部网页获取一个号码

<span>
<b>Maximum price:</b>
7 //value i need
</span>

然后显示该号码

I need to get a number from a external webpage

<span>
<b>Maximum price:</b>
7 //value i need
</span>

Then display said number

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

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

发布评论

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

评论(3

人间不值得 2024-10-17 02:16:03

如果你知道页面的地址,并且确定其内容在这段时间内不会改变,你可以通过 DOM 的方式找到数字。但我需要有关您的问题的更多详细信息。

If you know the address of the page and you are sure that their content will not change during the time, you can find the number by means of DOM. But I need more detail about your problem.

我爱人 2024-10-17 02:16:02

以下代码有效。我已使用此线程作为源页面。

<?php

// Read the whole file.
$lines = file('http://stackoverflow.com/questions/4573498/get-value-from-external-webpage-php-or-java');

// Go through every line ..
while ($line = array_shift($lines)) {

        // Stop when you find the label we're looking for.
        if (strpos($line, 'Maximum price') !== false) break;

}

// The next line has your value on it.
$line = array_shift($lines);

// Print the first word on the line.
$values = explode(' ', $line);
echo $values[0];

The following code works. I've used this thread as the source page.

<?php

// Read the whole file.
$lines = file('http://stackoverflow.com/questions/4573498/get-value-from-external-webpage-php-or-java');

// Go through every line ..
while ($line = array_shift($lines)) {

        // Stop when you find the label we're looking for.
        if (strpos($line, 'Maximum price') !== false) break;

}

// The next line has your value on it.
$line = array_shift($lines);

// Print the first word on the line.
$values = explode(' ', $line);
echo $values[0];
汐鸠 2024-10-17 02:16:00

在 Java 中,您可以使用 HTMLUnit 库。它擅长 HTML 提取。
例如类似的东西:

webClient=new WebClient();
HtmlPage page=webClient.getPage(url);
for(HtmlElement elem:page.getElementsByTagName("span")) {
   //And then getChildren(), getText ...
}

In Java you can use HTMLUnit library. It's good at HTML extraction.
For example something similar to:

webClient=new WebClient();
HtmlPage page=webClient.getPage(url);
for(HtmlElement elem:page.getElementsByTagName("span")) {
   //And then getChildren(), getText ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文