selenium - iframe sub-frame

Feature

  Nested web page, to switch to the sub-frame iframe, then further action

method

  browser.switch_to.iframe(iframe_element)

Example - Login qq-mail

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://mail.qq.com/')

# Switch to the sub-frame iframe 
login_frame = driver.find_element_by_id ( ' login_frame ' )
driver.switch_to.frame(login_frame)

# Username + Password + Log 
driver.find_element_by_id ( ' U ' ) .send_keys ( ' 2621470058 ' )
driver.find_element_by_id ( ' P ' ) .send_keys ( ' password ' )
driver.find_element_by_id('login_button').click()

# Reservation page record time 
the time.sleep (5 )

# Extracted data 
ELE = driver.find_element_by_id ( ' USERALIAS ' )
 Print (ele.text)
Code

Baidu translated to crack the case

aims

  Crack Baidu translation interface crawl translation result data

Implementation steps

  1, F12 capture, find json address, observe query parameters

1, POST Address: https://fanyi.baidu.com/ v2transapi
 2 , Form1 form data (multiple gripping changing fields)
    from : ZH
   two: one
   Sign: 54706.276099   # This is how to generate? 
   token: a927248ae7146c842bb4a94457ca35ee # basic fixed, but also to find ways to get

  2 , to crawl the JS file

The upper right corner - search - sign: - find a specific JS file (index_c8a141d.js) - formatted output

  3, to find a sign of the generated code in the JS

1 , in the search of the formatted output JS code: sign: JS code found as follows: sign: m (a),
 2 , by setting breakpoints, locate the m (a) function, i.e. a function of generating a specific sign
    # 1 the word to be translated as a 
   # 2 at the position of the mouse to move to m (a), a click-to DETAILED m (a) function block

  4, the generated sign m (a) as a function of the specific code (in a large define)

function a(r) {
        if (Array.isArray(r)) {
            for (var o = 0, t = Array(r.length); o < r.length; o++)
                t[o] = r[o];
            return t
        }
        return Array.from(r)
    }
function n(r, o) {
    for (var t = 0; t < o.length - 2; t += 3) {
        a var = ocharAt (t + 2 );
        a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
            a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
            r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
    }
    return r
}
function e(r) {
    was o = r.match (/ [\ uD800- \ uDBFF] [\ uDC00- \ uDFFF] / g);
    if (null === o) {
        var t = r.length;
        t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
    } else {
        for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
            "" !== e[C] && f.push.apply(f, a(e[C].split(""))),
                C !== h - 1 && f.push(o[C]);
        var g = f.length;
        g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
    }
Var = U // void 0
 //, L = "" + the String.fromCharCode (103) + the String.fromCharCode (1 16) + the String.fromCharCode (107 );
 // == null = U I I: (I!? window = [L] || "" ) || "" ;
 //   breakpoints, then locate the value from the web page source code window.gtk    
    var u = ' 320305.131321201 '
    
    for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
        var A = r.charCodeAt(v);
        128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
            S[c++] = A >> 18 | 240,
            S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
                                                                    S[c++] = A >> 6 & 63 | 128),
                                S[c++] = 63 & A | 128)
    }
    for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
        p += S[b],
            p = n(p, F);
    return p = n(p, D),
        p ^= s,
        0 > p && (p = (2147483647 & p) + 2147483648),
        p %= 1e6,
        p.toString() + "." + (p ^ m)
}
Code

  5, directly to the local code into js file, using module executed pyexecjs js code debugging

  Installation pyexecjs module

  sudo pip3 install pyexecjs

import execjs

with open('node.js','r') as f:
    js_data = f.read ()
 # create the object 
exec_object = execjs.compile (js_data)
sign = exec_object.eval('e("hello")')
print(sign)

  Get token

# In the js 
token: window.common.token
 # in response to find ways to get this value 
TOKEN_URL = ' https://fanyi.baidu.com/?aldtype=16047 ' 
regex: " token: '(.? *)' "

  Specific code implementation

import requests
import re
import execjs

class BaiduTranslateSpider(object):
    def __init__(self):
        self.token_url = 'https://fanyi.baidu.com/?aldtype=16047'
        self.post_url = 'https://fanyi.baidu.com/v2transapi'
        self.headers = {
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
            # 'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'cookie': 'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574 = 1563426293,1563996067; from_lang_often =% 5B% 7B% 22value% 22% 3A% 22zh% 22% 2C% 22text% 22% 3A% 22% u4E2D% u6587% 22% 7D% 2C% 7B% 22value% 22% 3A% 22en% 22% 2C % 22text% 22% 3A% 22% u82F1% u8BED% 22% 7D% 5D; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574 = 1563999768; yjs_js_security_passport = 2706b5b03983b8fa12fe756b8e4a08b98fb43022_1563999769_js',
            'pragma': 'no-cache',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        }

    # 获取token
    def get_token(self):
        TOKEN_URL = ' https://fanyi.baidu.com/?aldtype=16047 ' 
        # defined request header 
        R & lt requests.get = (self.token_url, headers = self.headers)
        token = re.findall(r"token: '(.*?)'",r.text)
        window_gtk = re.findall(r"window.*?gtk = '(.*?)';</script>",r.text)
        if token:
            return token[0],window_gtk[0]

    # Gets Sign 
    DEF get_sign (Self, Word):
        Open with ( ' Baidu translation .js ' , ' R & lt ' ) AS F:
            js_data = f.read()

        exec_object = execjs.compile(js_data)
        sign = exec_object.eval('e("{}")'.format(word))

        return sign

    # Main function 
    DEF main (Self, Word, FRO, to):
        token,gtk = self.get_token()
        Sign = self.get_sign (Word)
         # find form as the form data, sign and token need to find ways obtain 
        form_data = {
             ' from ' : FRO,
             ' to ' : to,
             ' Query ' : Word,
             ' TransType ' : ' Realtime ' ,
             ' simple_means_flag ' : ' . 3 ' ,
             ' Sign ' : Sign,
             ' token ': token
        }
        r = requests.post(self.post_url,data=form_data,headers=self.headers)
        print(r.json()['trans_result']['data'][0]['dst'])

if __name__ == '__main__':
    spider = BaiduTranslateSpider()
    MENU = ' 1. 2. Translation English to Chinese English ' 
    Choice = INPUT ( ' 1. 2. English to Chinese English Translation: ' )
    Word = the INPUT ( ' Enter word to be translated: ' )
     IF Choice == ' 1 ' :
        fro,to = 'en','zh'
    elif choice == '2':
        fro, to = ' zh ' , ' en '

    spider.main(word,fro,to)
Code

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11366058.html