zoukankan      html  css  js  c++  java
  • python使用chrome抓取页面中ajax请求返回的数据

    #-*-coding:utf-8-*-
    
    from time import sleep
    from selenium import webdriver
    import json
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    # 过滤出数据请求中的headers
    def getHttpInfo(browser):
        for responseReceived in browser.get_log('performance'):
            try:
                response = json.loads(responseReceived[u'message'])[u'message'][u'params'][u'response']
                if 'ajaxUrl' in response['url']:
                    # print(response)
                    # print(response['url'])
                    # print(response['headers'])
                    # print(response['headersText'])
                    return response['requestHeaders']
            except:
                pass
        return None
    
    # 请求页面 并设置headers到文件中
    def setHeaders():
        d = DesiredCapabilities.CHROME
        d['loggingPrefs'] = { 'performance':'ALL' }
        options=webdriver.ChromeOptions()
        options.set_headless()
        options.add_argument('--disable-gpu')
    
        driver=webdriver.Chrome(desired_capabilities=d,options=options)
        driver.get('http://www.baidu.com')
        sleep(20)
        headers = getHttpInfo(driver)
        driver.quit()
        # write header
        hand = open('header.txt', 'w')
        hand.write(json.dumps(headers))
        hand.close()
    
    if __name__ == '__main__':
        setHeaders()
    
  • 相关阅读:
    关于 JLRoutes
    关于Objection 框架或解耦合方案
    窥探 NSObject
    关于cocoa 运行时runtime
    关于 cocoapods 使用
    关于Xcode 遇到的 警告、错误 处理
    ios 中正则匹配 ,NSPredicate
    关于状态栏 上颜色配置 ios7.x 之后
    使用ios系统侧滑 7.x 之后
    常用的shell脚本
  • 原文地址:https://www.cnblogs.com/ningmo/p/10695876.html
Copyright © 2011-2022 走看看