I
I
Ibishka2020-01-06 17:16:12
JavaScript
Ibishka, 2020-01-06 17:16:12

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);
    }
  });

I need to get the content of an element document.querySelectorAll("json-widget-entry")[3];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2020-01-06
@Ibishka

There is no need to parse something. ipinfo.ioprovides 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")

Response contains more fields:
{
  "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"
}

S
Shohruh Shaimardonov, 2020-01-06
@joeberetta

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
  }
});

Z.s. you may not be able to reach the element you need, because it's just that as a result of xhr it may not be

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question