Answer the question
In order to leave comments, you need to log in
How to parse an element from another site?
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://ipinfo.io");
xhr.send();
xhr.addEventListener("load", function() {
if (xhr.status != 200) {
alert(`Ошибка ${xhr.status}: ${xhr.statusText}`);
} else {
alert(xhr.response);
}
});
Answer the question
In order to leave comments, you need to log in
There is no need to parse something. ipinfo.io
provides API , there is a free plan (up to 50 thousand requests per month)
For example, using jQuery, without authorization, you can get IP, country:
$.get("https://ipinfo.io", function(response) {
console.log(response.ip, response.country);
}, "jsonp")
{
"ip": "134.209.xxx.xxx",
"city": "Clifton",
"region": "New Jersey",
"country": "US",
"loc": "40.8344,-74.1377",
"org": "AS14061 DigitalOcean, LLC",
"postal": "07014",
"timezone": "America/New_York",
"readme": "https://ipinfo.io/missingauth"
}
This is how it should work:
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://ipinfo.io");
xhr.send();
xhr.addEventListener("load", function() {
if (xhr.status != 200) {
alert(`Ошибка ${xhr.status}: ${xhr.statusText}`);
} else {
let rawHTML = xhr.response;
let parsedDOM = new DOMParser().parseFromString(rawHTML, 'text/html').documentElement.childNodes[2]; // получаем сразу body
console.log(parsedDOM.querySelectorAll(".json-widget-entry")[3]); // поиск внутри тега body
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question