self_example/Spider/Chapter11_JavaScript逆向/浏览器环境执行JS/main_before.py

45 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2024/03/21 15:57
@Usage :
@Desc :使用playwright自动一个浏览器执行js
'''
import requests
from playwright.sync_api import sync_playwright
import time
import os
BASE_URL = 'https://spa2.scrape.center'
INDEX_URL = BASE_URL + "/api/movie?limit={limit}&offset={offset}&token={token}"
MAX_PAGE = 10
LIMIT = 10
context = sync_playwright().start()
browser = context.chromium.launch()
page = browser.new_page()
# 注意这里路径需要加上**
page.route(
"/js/chunk-10192a00.243cb8b7.js",
lambda route: route.fulfill(path='chunk.js')
)
page.goto(BASE_URL)
def get_token(offset):
result = page.evaluate('''() => {
return window.encrypt("%s","%s")
}''' % ('/api/movie', offset))
return result
for i in range(MAX_PAGE):
offset = i * LIMIT
token = get_token(offset)
print(token)
index_url = INDEX_URL.format(limit=LIMIT, offset=offset, token=token)
response = requests.get(index_url)
print('response', response.json())