1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| import re import asyncio
import pyppeteer from pyppeteer import launcher
if pyppeteer.version <= "0.0.25": launcher.AUTOMATION_ARGS.remove("--enable-automation")
from pyppeteer import launch
from pyppeteer.network_manager import Request, Response from pyppeteer.dialog import Dialog
proxy = "http://127.0.0.1:1080"
args = [ "--start-maximized", "--no-sandbox", "--ignore-certificate-errors", "--log-level=3", "--enable-extensions", "--window-size=1920,1080", "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36", ]
if pyppeteer.version <= "0.0.25": args.append("--disable-infobars") else: args.append("--disable-blink-features=AutomationControlled")
launch_args = { "headless": False, "args": args, "autoClose": False, "dumpio": True, }
if pyppeteer.version > "0.0.25": launch_args.update( {"ignoreDefaultArgs": ["--enable-automation", "--disable-extensions"]} )
async def modify_url(request: Request): """ # 启用拦截器 await page.setRequestInterception(True) page.on("request", use_proxy_base) :param request: :return: """ if request.url == "https://www.baidu.com/": await request.continue_({"url": "https://www.baidu.com/s?wd=ip&ie=utf-8"}) else: await request.continue_()
async def get_content(response: Response): """ # 注意这里不需要设置 page.setRequestInterception(True) page.on("response", get_content) :param response: :return: """ if response.url == "https://www.baidu.com/": content = await response.text() title = re.search(b"<title>(.*?)</title>", content) print(title.group(1))
async def handle_dialog(dialog: Dialog): """ page.on("dialog", get_content) :param dialog: :return: """ await dialog.dismiss()
import aiohttp
aiohttp_session = aiohttp.ClientSession(loop=asyncio.get_event_loop())
async def use_proxy_base(request: Request): """ # 启用拦截器 await page.setRequestInterception(True) page.on("request", use_proxy_base) :param request: :return: """ req = { "headers": request.headers, "data": request.postData, "proxy": proxy, "timeout": 5, "ssl": False, } try: async with aiohttp_session.request( method=request.method, url=request.url, **req ) as response: body = await response.read() except Exception as e: await request.abort() return
resp = {"body": body, "headers": response.headers, "status": response.status} await request.respond(resp) return
static_cache = {}
async def use_proxy_and_cache(request: Request): """ # 启用拦截器 await page.setRequestInterception(True) page.on("request", use_proxy_base) :param request: :return: """ global static_cache if request.url not in static_cache: req = { "headers": request.headers, "data": request.postData, "proxy": proxy, "timeout": 5, "ssl": False, } try: async with aiohttp_session.request( method=request.method, url=request.url, **req ) as response: body = await response.read() except Exception as e: await request.abort() return
resp = {"body": body, "headers": response.headers, "status": response.status} content_type = response.headers.get("Content-Type") if content_type and ("javascript" in content_type or "/css" in content_type): static_cache[request.url] = resp else: resp = static_cache[request.url]
await request.respond(resp) return
async def pass_webdriver(request: Request): """ # 启用拦截器 await page.setRequestInterception(True) page.on("request", use_proxy_base) :param request: :return: """ req = { "headers": request.headers, "data": request.postData, "proxy": proxy, "timeout": 5, "ssl": False, } try: async with aiohttp_session.request( method=request.method, url=request.url, **req ) as response: body = await response.read() except Exception as e: await request.abort() return
if request.url == "https://www.baidu.com/": with open("pass_webdriver.js") as f: js = f.read() body = body.replace(b"<title>", b"<script>%s</script><title>" % js.encode())
resp = {"body": body, "headers": response.headers, "status": response.status} await request.respond(resp) return
async def interception_test(): browser = await launch(**launch_args) page = await browser.newPage() page.setDefaultNavigationTimeout(10 * 1000) await page.setViewport({"width": 1920, "height": 1080})
if 0: await page.setRequestInterception(True) if pyppeteer.version <= "0.0.25": page.on("request", modify_url) else: page.on("request", lambda r: asyncio.ensure_future(modify_url(r))) if 0: if pyppeteer.version <= "0.0.25": page.on("response", get_content) else: page.on("response", lambda r: asyncio.ensure_future(get_content(r))) if 0: await page.setRequestInterception(True) if pyppeteer.version <= "0.0.25": page.on("request", pass_webdriver) else: page.on("request", lambda r: asyncio.ensure_future(pass_webdriver(r)))
await page.goto("http://www.baidu.com")
await asyncio.sleep(10)
await page.close() await browser.close() return
if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(interception_test())
|