A
A
AKurmazov2017-06-13 12:47:05
Python
AKurmazov, 2017-06-13 12:47:05

How to parse a site using Ajax?

Hello. There is a task to parse the site www.realestate.ru/flatrent/4275750 , one of the parameters is the owner's phone number, but it is loaded using Ajax. When you click on the "Show phone" button, a post request is sent and in the Network tab in the Response section it only says "OK"

Question: How can I get this phone using Python and requests?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-06-13
@AKurmazov

See the source of the page, pick up a notepad and a calculator and look for what kind of obsfucation is here.
Specifically, on this site, everything is simple:
Find your line with "Show phone":
We are looking for where these blst , lst1 , lst2 are used and find

function ShowPhones(e)
{
  var blst = $(e).attr('blst');
  var lst1 = $(e).attr('lst1');
  var lst2 = $(e).attr('lst2');
  if (blst != "0")
  {
    $('.object-builder-phone').each(function ()
    {
      <b>ShowAttr(this, "blst", blst);</b>
...

We are looking for ShowAttr and find the decoder:
function ShowAttr(e, attr, value)
{
  if ($(e).attr(attr) == "true")
  {
    var decrypt = value / 17;
    var p1 = Math.floor(decrypt / 100);
    var p2 = decrypt - 100 * p1;
    var t1 = ("" + p1).substring(1) + '-' + pad(p2, 2);
    $(e).text($(e).text().replace("...", "") + t1);
    $(e).attr(attr, 'false');
  }
}

so it turns out that blst=" 313548 ", divide by 17, we get 313548/17 = 18444
split into two parts, remove the first character, add a hyphen between them, we get: 84-44
Total +7 495 626-... converted at +7 495 626-84-44
PS You can even follow the logic of a programmer who amusingly optimized the formatting of the first two of numbers - in order not to call the pad function additionally to format zeros, he simply added 10000 so that zeros would simply be, and they could be taken through substring. This is not possible for the second two of numbers, so pad is called there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question