需要解析其他网页的值.首先,我需要调用其他网页并从中解析 XML 值
Need to parse a value from other webpage. First I need to call other webpage and parse the XML value from it
我正在开发一个应该显示货币汇率的项目,所以我计划调用另一个网页来从该页面获取汇率值。 我尝试使用Angular-js,但我无法从网页获得响应(在Angular JS中:我们只能调用JSON/Rest url)。我尝试过XMLHttpRequest,但是如果我们从其他域(CORS的Beacuse)调用网页,它不会调用网页(url)。
同样,我尝试使用 Java 并成功调用网页并获取 XML,但我无法解析值(收到错误:"未格式化的 XML")。
有人可以指导我,我如何从任何网页中获得价值。请让我知道我是否可以通过使用 API 调用或任何 Web 服务调用来实现。如果我使用 API 或 Web 服务调用,那么我是否需要与货币兑换网站的 IT 供应商进行通信,以便让 API/Web 服务使用特定值??.
请帮助我(我准备在任何技术上实施)
爪哇代码:
包 webXMRead;
import java.io.IOException; 导入java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformURLException; import java.net.URISyntaxException; 导入java.net.URL;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList;
公共类网页XMLRead { public static void main(String args[]) 抛出 URISyntaxException, ClientProtocolException, IOException, MalformURLException {
出于研究和示例目的,我选择了网址:http://www.google.com,需要解析本网站,我不用于任何盈利目的
字符串网址 ="http://www.google.com"; System.out.println("Url is careated****"); 网址 url2 = 新网址(网址); HttpGet httpGet = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient();HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); System.out.println("Entity is*****" + entity); try { String xmlParseString = EntityUtils.toString(entity); System.out.println("This Stirng ***" + xmlParseString); HttpURLConnection connection = (HttpURLConnection) url2 .openConnection(); InputStream inputStream = connection.getInputStream(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = builderFactory .newDocumentBuilder(); Document document = documentBuilder.parse(inputStream); document.getDocumentElement().normalize(); NodeList nodeList = document.getElementsByTagName("rss"); System.out.println("This is firstnode" + nodeList); for (int getChild = 0; getChild < nodeList.getLength(); getChild++) { Node Listnode = nodeList.item(getChild); System.out.println("Into the for loop" + Listnode.getAttributes().getLength()); Element firstnoderss = (Element) Listnode; System.out.println("ListNodes" + Listnode.getAttributes()); System.out.println("This is node list length" + nodeList.getLength()); Node Subnode = nodeList.item(getChild); System.out.println("This is list node" + Subnode); } } catch (Exception exception) { System.out.println("Exception is" + exception); } }
Angular-JS:(我只是试图检查它是否返回任何值,但没有成功。但是当我在不同的域尝试时,我在XMLHttpRequest(javascript)中遇到了CORS问题)
角度-JS代码:
<!DOCTYPE html>
<html>
<head>
<title>test your webservice</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<article ng-app="webpage">
<section ng-controller="booksCtrl">
<h2 >{{data}} </h2>
</section>
</article>
<script type="text/javascript">
var app = angular.module('webpage', []);
app.controller('booksCtrl', function($scope, $http) {
/* $httpProvider.defaults.useXDomain = true;*/
/*delete $http.defaults.headers.common['X-Requested-With'];*/
/*just for study purpose, not for any profit usage, so for example purpose I used URL:http://www.google.com, */
$http.get("http://www.google.com")
.then(function(response) {
$scope.data=response.data;
},
function(errresponse) {
alert("err"+errresponse.status);
});
});
</script>
</body>
</html>
基本上你需要解析一个HTML文档。为此,请使用JSoup。这将是适合您的四个用例的理想之选。在 java 中拥有 Document 对象后,您可以解析并从中获取所需的值。
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
相关文章:
- 网页上失败的javascript会导致所有其他脚本失败
- 在php中的同一网页上检索所选下拉项的值,并使用其他数据将其发布到其他表单
- 使用 jquery 在我的网站中加载其他网页不起作用
- 如何从电子邮件内容中调用具有隐藏参数的其他网页
- 如何从其他网页获取图像并在我的网站上显示
- 如何使用其他名称调用网页
- 需要解析其他网页的值.首先,我需要调用其他网页并从中解析 XML 值
- 从其他网站单击网页上的按钮时刷新外部网页
- 需要检测网页中哪个<脚本>块与哪些其他<脚本>块交互
- 如何使用 JavaScript 将网页的元素复制到其他网页
- Javascript 函数创建自己的 ,但在此过程中删除网页上的所有其他
- 在其他网页中将图表和选择框作为“小部件”包含在内
- 如何在单击输入时重定向到其他网页
- 从其他网页打开模式网页
- 张贴表格,然后下载其他网页
- 如何在我的网页上显示JavaScript和其他代码,以便可以复制
- 如何将图像放置在网页上其他图像的指定像素处
- 当用户在网页上选择一大块文本时,是否有一种方法可以使该文本保持高亮显示,即使用户在该页面的其他地方单击也是如此
- 如果网页没有HTML/JavaScript格式,请转到其他链接
- 当抓取使用Javascript(也许还有其他东西)的网页时