browser_server.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:jtyd_python_spider 作者: xtuyaowu 项目源码 文件源码
def get(self, url, xpath, timeout, retry, service_args, desired_capabilities):
        browser = None
        try:
            result = dict()
            if desired_capabilities:
                DesiredCapabilities.PHANTOMJS.update(json.loads(desired_capabilities))
            browser = webdriver.PhantomJS(service_args=json.loads(service_args) if service_args else None,
                                          desired_capabilities=DesiredCapabilities.PHANTOMJS)
            count = 0
            while (retry + 1) > count:
                count += 1
                try:
                    browser.get(url)
                    break
                except Exception as e:
                    print str(e)
            if xpath:
                browser.implicitly_wait(timeout)
                try:
                    browser.find_element_by_xpath(xpath)
                except Exception as e:
                    print str(e)
            text = browser.page_source
            if text == '<html><head></head><body></body></html>':
                browser.quit()
                return ''
            result['cookies'] = browser.get_cookies()
            result['text'] = text.encode('utf-8')
            browser.quit()
            return json.dumps(result)
        except Exception as e:
            if browser:
                browser.quit()
            print str(e)
            return ''
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号