コード例 #1
0
async def _action_combo_get_page_content(url, cookies_dir='data/cookies/'):
    try:
        #解析url属于那个domain
        parsed_uri = urlparse(url)
        cookies_file = "".join([cookies_dir, parsed_uri.netloc, "cookie"])
        my_cookie_file = DataFile.read_file_intostr(cookies_file)
        browser = await launch({
            "executablePath": "chromium-browser",
            "args": ["--no-sandbox"]
        })
        page = await browser.newPage()
        #读取cookies
        if (len(my_cookie_file) > 0):
            my_cookie_object = json.loads(my_cookie_file)
            print("".join(
                ["Load ",
                 str(len(my_cookie_object)), " cookie item(s)."]))
            for row in my_cookie_object:
                await page.setCookie(row)
        #设置UA
        ua_box = UserAgent.UserAgentBox()
        await page.setUserAgent(ua_box.wap_normal_user)
        await page.goto(url)
        new_cookie = await page.cookies()
        json_cookie = json.dumps(new_cookie)
        res = await action_get_page_content(page)
        DataFile.write_full_file(cookies_file, json_cookie)
        await browser.close()
        return res
    except Exception as e:
        traceback.print_exc()
        return ""