gpt4 book ai didi

javascript - 需要解析其他网页的值。首先我需要调用其他网页并从中解析 XML 值

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:32 25 4
gpt4 key购买 nike

我正在开发一个应该显示货币汇率的项目,因此我计划调用另一个网页以从该页面获取汇率值。我在 Angular-js 中尝试过,但无法从网页获得响应(在 Angular JS 中:我们只能调用 JSON/Rest url )。我在 XMLHttpRequest 中尝试过,但如果我们从其他域调用网页,它不会调用网页(url)(因为 CORS )。

类似地,我在 Java 中进行了尝试,成功调用了网页并获取了 XML,但无法解析该值(出现错误:“未格式化的 XML”)。

有人可以指导我,如何从任何网页获取值(value)。请让我知道我是否可以通过使用 API 调用或任何 Web 服务调用来实现。如果我使用 API 或 Web 服务调用,那么我是否需要与 Moneyexchange 网站的 IT vendor 进行通信,以便让 API/Web 服务使用特定的值?

请帮助我(我已准备好实现任何技术)

Java代码:

    package webXMRead;
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import 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;
public class webPageXMLRead { public static void main(String args[]) throws URISyntaxException, ClientProtocolException, IOException, MalformedURLException {
//For study and example purpose I took url:http://www.google.com , need to parse this website, I am not using for any profit purpose
String url = "http://www.google.com"; System.out.println("Url is careated****"); URL url2 = new URL(url); 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 问题)

Angular-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 - 需要解析其他网页的值。首先我需要调用其他网页并从中解析 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com