# 浏览器抓取当前网站Cookie插件 - Cookie-Editor
import requests
from bs4 import BeautifulSoup
from retrying import retry
@retry(stop_max_attempt_number=3)
def crawler(url, params, json, headers, cookies, proxies, verify, timeout, allow_redirects)
resp = requests.post(url, params, json, headers, cookies, proxies, verify, timeout, allow_redirects)
resp.raise_for_status()
resp.encoding = "utf-8"
resp.content.decode("unicode-escape")
resp.request
resp.status_code
resp.text
resp.content
resp.json()
def obtain_resource(html_text):
document = BeautifulSoup(html_text, "html.parser")
element = document.select_one("selector")
elements = document.select("selector")
init_script = """
window.chrome = { runtime: {} };
Object.defineProperty(navigator, "webdriver", { get: () => false });
Object.defineProperty(navigator, 'languages', { get: () => ['zh-CN','zh','en'] });
Object.defineProperty(navigator, 'plugins', {
get: () => {
const pluginList = [
{
name: "PDF Viewer",
filename: "internal-pdf-viewer",
description: "Portable Document Format",
length: 1,
0: {
type: "application/pdf",
suffixes: "pdf",
description: "PDF document",
enabledPlugin: null
}
},
{
name: "Chrome PDF Viewer",
filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai",
description: "",
length: 1,
0: {
type: "application/pdf",
suffixes: "pdf",
description: "PDF document",
enabledPlugin: null
}
},
{
name: "Chromium PDF Viewer",
filename: "internal-pdf-viewer",
description: "Portable Document Format",
length: 1,
0: {
type: "application/pdf",
suffixes: "pdf",
description: "PDF document",
enabledPlugin: null
}
},
{
name: "Microsoft Edge PDF Viewer",
filename: "internal-pdf-viewer",
description: "Portable Document Format",
length: 1,
0: {
type: "application/pdf",
suffixes: "pdf",
description: "PDF document",
enabledPlugin: null
}
},
{
name: "WebKit built-in PDF",
filename: "internal-pdf-viewer",
description: "Portable Document Format",
length: 1,
0: {
type: "application/pdf",
suffixes: "pdf",
description: "PDF document",
enabledPlugin: null
}
}
]
pluginList.item = function (idx) { return this[idx] || null }
pluginList.namedItem = function (name) {
return this.find(p => p.name === name) || null
}
return pluginList
}
});
"""
from playwright.sync_api import sync_playwright
# async with async_play() as pw -- asyncio.run(async_method)
with sync_playwright() as pw:
browser = pw.chromium.launch(
headless=False,
slow_mo=1200,
args=["--disable-blink-features=AutomationControlled"]
)
context = browser.new_context(storage_state="browser_state.json")
page = browser.new_page()
page.add_init_script(init_script)
page.goto("https://www.baidu.com", wait_until="networkidle")
cookies = context.cookies()
context.add_cookies(cookies)
context.storage_state(path="browser_state.json") # 获取storage_state数据 - cookies + storage
element = page.query_selector("selector")
elements = page.query_selector_all("selector")
attr = element.get_attribute("attr") # 校验非None
location = page.get_by_text("文本").nth(0)
location.evaluate("element => element.click()")
page.get_by_text("文本").nth(0).click()
page.evaluate("() => { window.open("https://xxx.xx") }")
page.keyboard.type("文本", delay=360)
page.keyboard.press("Enter")
page.set_input_files("selector", "file_path")
page.screenshot(path="screenshot.png", full_page=True)
page.wait_for_selector("selector", timeout=5000)
page.wait_for_timeout(1000)
html_text = page.content()
page.set_content(html_text)
page.pdf(path="pdf_file.pdf", format="A4")
with page.expect_popup() as popup_page:
"""监听弹出页面事件 - 事件触发"""
pass
new_page = popup_page.value
input("暂停代码, 按回车键继续")
page.pause() # 停止代码运行, 显示辅助工具
browser.close()