Qwentor
@Qwentor
Веб-программист

Как подделать локальное время в pyppeteer?

Как подделать локальное время в pyppeteer? Чтобы сайты видели время, соответствующее часовому поясу прокси.
  • Вопрос задан
  • 109 просмотров
Решения вопроса 1
@Tanomlette
Как сам только что столкнувшийся с проблемой, помогаю тем, кто будет здесь после меня.
Суть решения в том, чтобы запустить JS скрипт изменяющий время/часовой пояс используя evaluateOnNewDocument.
Пример рабочего кода: (включает в себя скрипт TimeShift)

import asyncio
from pyppeteer import launch

time_shift_js = '''/*!
 * TimeShift.js
 *
 * Copyright 2013 Mobile Wellness Solutions MWS Ltd, Sampo Juustila
 * Released under the MIT license
 */

() =>{ 

(function() {

    var root = this;
    var OriginalDate = root.Date;

    var TimeShift;
    if (typeof exports !== 'undefined') {
        TimeShift = exports;
    } else {
        TimeShift = root.TimeShift = {};
    }


    var currentTime = undefined;
    var timezoneOffset = new OriginalDate().getTimezoneOffset();

    function currentDate() {
        if (currentTime) {
            return new OriginalDate(currentTime);
        } else {
            return new OriginalDate();
        }
    }


    function realLocalToUtc(realLocal) {
        return new OriginalDate(realLocal.getTime() - realLocal.getTimezoneOffset()*60*1000 + timezoneOffset*60*1000);
    }
    function utcToLocal(utc) {
        return new OriginalDate(utc.getTime() - timezoneOffset*60*1000);
    }
    function localToUtc(local) {
        return new OriginalDate(local.getTime() + timezoneOffset*60*1000);
    }
    function twoDigit(n) {
        if (n < 10) {
            return "0" + n;
        } else {
            return "" + n;
        }
    }
    function timezoneName() {
        var zone = "GMT";
        var offset = Math.abs(timezoneOffset);
        if (timezoneOffset < 0) {
            zone = zone + "+";
        } else if (timezoneOffset > 0) {
            zone = zone + "-";
        } else {
            return zone;
        }
        return zone + twoDigit(Math.floor(offset/60)) + twoDigit(offset%60);
    }
    TimeShift.getTimezoneOffset = function() {
        return timezoneOffset;
    }
    TimeShift.setTimezoneOffset = function(offset) {
        timezoneOffset = offset;
    }
    TimeShift.getTime = function() {
        return currentTime;
    }
    TimeShift.setTime = function(time) {
        currentTime = time;
    }
    TimeShift.OriginalDate = OriginalDate;
    TimeShift.Date = function() {
        var isConstructor = false;
        if (this instanceof TimeShift.Date && !this.__previouslyConstructedByTimeShift) {
            isConstructor = true;
            this.__previouslyConstructedByTimeShift = true;
        }
        if (!isConstructor) {
            return (new TimeShift.Date()).toString();
        }

        switch (arguments.length) {
        case 0:
            this.utc = currentDate();
            break;
        case 1:
            this.utc = new OriginalDate(arguments[0]);
            break;
        }
    }

    TimeShift.Date.prototype.getDate = function() { return utcToLocal(this.utc).getUTCDate(); }
    TimeShift.Date.prototype.getDay = function() { return utcToLocal(this.utc).getUTCDay(); }
    TimeShift.Date.prototype.getFullYear = function() { return utcToLocal(this.utc).getUTCFullYear(); }
    TimeShift.Date.prototype.getHours = function() { return utcToLocal(this.utc).getUTCHours(); }
    TimeShift.Date.prototype.getMilliseconds = function() { return utcToLocal(this.utc).getUTCMilliseconds(); }
    TimeShift.Date.prototype.getMinutes = function() { return utcToLocal(this.utc).getUTCMinutes(); }
    TimeShift.Date.prototype.getMonth = function() { return utcToLocal(this.utc).getUTCMonth(); }
    TimeShift.Date.prototype.getSeconds = function() { return utcToLocal(this.utc).getUTCSeconds(); }

    TimeShift.Date.prototype.getUTCDate = function() { return this.utc.getUTCDate(); }
    TimeShift.Date.prototype.getUTCDay = function() { return this.utc.getUTCDay(); }
    TimeShift.Date.prototype.getUTCFullYear = function() { return this.utc.getUTCFullYear(); }
    TimeShift.Date.prototype.getUTCHours = function() { return this.utc.getUTCHours(); }
    TimeShift.Date.prototype.getUTCMilliseconds = function() { return this.utc.getUTCMilliseconds(); }
    TimeShift.Date.prototype.getUTCMinutes = function() { return this.utc.getUTCMinutes(); }
    TimeShift.Date.prototype.getUTCMonth = function() { return this.utc.getUTCMonth(); }
    TimeShift.Date.prototype.getUTCSeconds = function() { return this.utc.getUTCSeconds(); }

    TimeShift.Date.prototype.setDate = function() { var d = utcToLocal(this.utc); d.setUTCDate.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setFullYear = function() { var d = utcToLocal(this.utc); d.setUTCFullYear.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setHours = function() { var d = utcToLocal(this.utc); d.setUTCHours.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setMilliseconds = function() { var d = utcToLocal(this.utc); d.setUTCMilliseconds.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setMinutes = function() { var d = utcToLocal(this.utc); d.setUTCMinutes.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setMonth = function() { var d = utcToLocal(this.utc); d.setUTCMonth.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }
    TimeShift.Date.prototype.setSeconds = function() { var d = utcToLocal(this.utc); d.setUTCSeconds.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); }

    TimeShift.Date.prototype.setUTCDate = function() { this.utc.setUTCDate.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCFullYear = function() { this.utc.setUTCFullYear.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCHours = function() { this.utc.setUTCHours.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCMilliseconds = function() { this.utc.setUTCMilliseconds.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCMinutes = function() { this.utc.setUTCMinutes.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCMonth = function() { this.utc.setUTCMonth.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }
    TimeShift.Date.prototype.setUTCSeconds = function() { this.utc.setUTCSeconds.apply(this.utc, Array.prototype.slice.call(arguments, 0)); }


    TimeShift.Date.prototype.getYear = function() { return this.getFullYear() - 1900; }
    TimeShift.Date.prototype.setYear = function(v) { this.setFullYear(v + 1900); }

    TimeShift.Date.prototype.getTime = function() { return this.utc.getTime(); }
    TimeShift.Date.prototype.setTime = function(v) { this.utc.setTime(v); }

    TimeShift.Date.prototype.getTimezoneOffset = function() { return timezoneOffset; }

    TimeShift.Date.prototype.toDateString = function() { return utcToLocal(this.utc).toDateString(); }  // Wrong
    TimeShift.Date.prototype.toLocaleDateString = function() { return utcToLocal(this.utc).toLocaleDateString(); }  // Wrong

    TimeShift.Date.prototype.toISOString = function() { return this.utc.toISOString(); }
    TimeShift.Date.prototype.toGMTString = function() { return this.utc.toGMTString(); }
    TimeShift.Date.prototype.toUTCString = function() { return this.utc.toUTCString(); }

    TimeShift.Date.prototype.toString = function() {
        var wkdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var d = utcToLocal(this.utc);
        return wkdays[d.getUTCDay()] + " " + months[d.getUTCMonth()] + " " + twoDigit(d.getUTCDate()) + " " + d.getUTCFullYear() +
            " " + twoDigit(d.getUTCHours()) + ":" + twoDigit(d.getUTCMinutes()) + ":" + twoDigit(d.getUTCSeconds()) + " " + timezoneName();
    }
    TimeShift.Date.prototype.toLocaleString = function() { return this.toString(); }  // Wrong
    TimeShift.Date.prototype.toLocaleTimeString = function() { return this.toString(); }  // Wrong
    TimeShift.Date.prototype.toTimeString = function() { return this.toString(); }  // Wrong

    TimeShift.Date.prototype.toJSON = function() { return this.utc.toJSON(); }
    TimeShift.Date.prototype.valueOf = function() { return this.utc.getTime(); }


    TimeShift.Date.now = function() { return currentDate().getTime(); }
    TimeShift.Date.parse = OriginalDate.parse;  // Wrong
    TimeShift.Date.UTC = OriginalDate.UTC;


    /**
     * Helper method that describes a Date object contents.
     */
    TimeShift.Date.prototype.desc = function() {
        return "utc=" + this.utc.toUTCString() + "   local=" + utcToLocal(this.utc).toUTCString() + "   offset=" + timezoneOffset;
    }

}).call(typeof global !== 'undefined' ? global : this);

Date = TimeShift.Date; 
TimeShift.setTimezoneOffset(0);
}'''

async def main():
    browser = await launch(headless=False)
    page = await browser.newPage()

    await page.setExtraHTTPHeaders({'Accept-Language': 'en-US,en;q=0.9'})
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36')

    await page.evaluateOnNewDocument(time_shift_js)
    await page.goto("https://amiunique.org/fp")

    input()

    await browser.close()
asyncio.get_event_loop().run_until_complete(main())
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
03 мая 2024, в 00:45
1000 руб./за проект
02 мая 2024, в 23:56
2000 руб./за проект
02 мая 2024, в 23:29
1500 руб./в час