Python 抓取Chrome数据并且排序

<script type="text/javascript">
    window.initialWindowWidth = 600;
    window.initialWindowHeight = 600;
script>
<script type="text/javascript"
        src="/decision/view/form?op=emb&resource=finereport.js&inter=zh_TW&__fr_locale__=&jsVersion=1679989198962">script>
<script type="text/javascript"
        src="/decision/view/form?op=emb&resource=finereport.chart.js&jsVersion=1679989198962">script>
<script type="text/javascript"
        src="/decision/view/form?op=resource&resource=/com/fr/web/core/js/socket.io.js">script>



<style type="text/css">
    body.dragging, body.dragging * {
        cursor: move !important;
    }

    .dragged {
        position: absolute;
        opacity: 1;
        z-index: 2000;
    }

    #paramsTemplatePane > div {
        display: inline;
    }
style>




<script type="text/javascript">
    
script>
<script type="text/javascript" src="/decision/v1/cloud/file?resource=polyfill.min.js">script><script type="text/javascript" src="/decision/v1/cloud/file?resource=fp.min.js">script><script type="text/javascript" src="/decision/v1/cloud/file?resource=fine.mark.init.js">script>





<script type="text/javascript">
    //ie下低版本时修改浏览器渲染模式
    if ($.browser.msie && $.browser.version <= 8.0) {
        $("meta[http-equiv='X-UA-Compatible']").attr("content", "IE=5, IE=EmulateIE7, chrome=1");
    }
    $(function () {
        var $body = $('body');
        $('
'
).attr('name', 'Revenue_Reports_BG').appendTo($body); var items = []; var scrollHeight = 6; var expandHeight = 8; var contenConfig = {"type":"border","widgetName":"FORM","disabled":false,"invisible":false,"widgetUrl":"/decision/view/form?op=widget&widgetname=form&sessionID=0e260dc4-da2a-40fd-ac6d-d82a86724547","useBookMark":true,"bookMarkName":"form","border":{"borderRadius":0},"vgap":0,"hgap":0,"compInterval":0,"scrollable":false,"items":[{"region":"center","el":{"type":"horizontal","disabled":false,"invisible":false,"widgetUrl":"/decision/view/form?op=widget&widgetname=null&sessionID=0e260dc4-da2a-40fd-ac6d-d82a86724547","useBookMark":false,"bookMarkName":"","border":{"borderRadius":0},"vgap":0,"hgap":0,"compInterval":0,"scrollable":false,"items":[],"showBookmarks":false,"alignment":"center","displayposition":0}}],"showBookmarks":true}; var paraConfig = {}; var contentContainer = $("
"); var delayDisplay = paraConfig['delayDisplayContent']; contenConfig.renderEl = contentContainer; form = new FR.contentForm(contenConfig); FR.SessionMgr.register("0e260dc4-da2a-40fd-ac6d-d82a86724547", form); items.push({ region: 'center', el: contentContainer }); window.globalForm = form; var barHeight = paraConfig.width > document.body.offsetWidth ? scrollHeight : 0; FR.$defaultImport('/com/fr/web/core/js/paramtemplate.js', 'js'); var paramstemplateHeight = 30; var north = { region: 'north', height: paramstemplateHeight, el: { type: 'border', widgetName: 'paramsTemplate', items: [ { region: 'east', width: 138, el: { type: 'horizontal', alignment: 'right', items: [{ el: { type: 'button', widgetName: 'saveBtn', text: FR.i18nText("Fine-Engine_Report_Designer_Save_As_Params_Template") }, width: 128 }] } }, { region: 'center', el: { type: 'horizontal', widgetName: 'paramsTemplatePane', width: paraConfig.width - 120, minWidth: paraConfig.width - 120, alignment: 'left', items: [] } }, { region: 'west', width: 30, el: { type: 'horizontal', alignment: 'left', items: [ { el: { type: 'iconbutton', widgetName: 'newParamsBtn', width: 30, baseClass: 'fs-new-params-template-blue' }, width: 30 } ] } } ] } }; var paramsTemplate = getParamsTemplate(form.sessionID); if (!paraConfig.useParamsTemplate || FR.isEmpty(paramsTemplate)) { paramstemplateHeight = 0; north = {}; } if (!$.isEmptyObject(paraConfig)) { var paraContainer = $("
"); var alignLayoutName = '__layout4align__'; paraContainer.height(paraConfig.height + expandHeight + barHeight + paramstemplateHeight); var $expandEl = $("
"
); bindParaCollapse($expandEl); var $center = $('
'
); var south = { region: 'south', height: expandHeight, el: { type: 'horizontal', items: [ {el: $expandEl, width: 120} ] } }; var center = { region: 'center', el: { type: 'horizontal', widgetName: alignLayoutName, alignment: paraConfig.position, items: [ {el: $center, width: paraConfig.width} ] } }; var itemlst = [north, south, center]; var pCon = { renderEl: paraContainer, type: 'border', items: itemlst }; //有个默认的背景 var bg = FR.isEmptyObj(paraConfig.parambg) ? {background: '#f7f8fa'} : paraConfig.parambg; FR.setBackground(paraContainer, bg); paraConfig.renderEl = $center; window.globalForm.parameterEl = new FR.contentForm(paraConfig); items.push({ region: 'north', el: pCon }); } /** * 判断尺寸,在尺寸为0的情况下返回一个默认值 * 在某些情况下,如平台快速点击多个报表时,无法获取到尺寸, * 提供一个默认值, * * @param length 默认尺寸 * @param defaultValue 默认值 * @returns {*} */ function checkSizeOrDefault(length, defaultValue) { if (isNaN(defaultValue)) { return length; } return length === 0 ? defaultValue : length; } var width = checkSizeOrDefault(FR.windowWidth, window.location.href.getQuery("width")); var height = checkSizeOrDefault(FR.windowHeight, window.location.href.getQuery("height")); var borderLayout = new FR.BorderLayout({ items: items, renderEl: $body, width: width, height: height }); //给参数界面添加横向滚动条 var showParamsTemplate = paraConfig.useParamsTemplate && paramsTemplate !== null; addScollBar(paraContainer, $center, paraConfig.height + (showParamsTemplate ? 30 : 0), scrollHeight); borderLayout.doLayout(); $('[widgetname^=sortable]').removeAttr('style'); initParameterTemplate(paraConfig, paraConfig.items, form.sessionID, borderLayout, paramsTemplate); $(window).resize(function () { borderLayout.element.bounds({ 'width': FR.windowWidth, 'height': FR.windowHeight }); if (!form.scaleProcess(borderLayout, width, height)) { addScollBar(paraContainer, $center, paraConfig.height + (showParamsTemplate ? 30 : 0), scrollHeight); borderLayout.doLayout(); $('[widgetname^=sortable]').removeAttr('style'); } width = FR.windowWidth; height = FR.windowHeight; }); // 整个form都doLayout一下 var browser = $.browser; //b:for chrome,find reason if (browser.safari && browser.webkit || browser.msie && browser.version < 7) { $(window).trigger("resize"); $body.css('overflow', 'auto'); } // FR.Layout 有this.element.css("overflow","hidden"); //放大后再缩小, body和contentContainer大小一致, 却出现了滚动条, 很难看. contentContainer.css('overflow', 'auto'); if (!delayDisplay) { form.parameterCommit(); } function addScollBar(container, mover, top, height) { if (container) { container.hScrollPane({ mover: mover, //指定container对象下的哪个元素需要滚动位置 | 必传项; showArrow: false, //指定是否显示左右箭头,默认不显示 | 可选项; top: top, //滚动条所在的top位置 height: height,//滚动条的高度 //moverW:function(){return $(".press").width();}(), //传入水平滚动对象的长度值,不传入的话默认直接获取mover的宽度值 | 可选项; //handleMinWidth:100,//指定handle的最小宽度,要固定handle的宽度请在css中设定handle的width属性(如 width:28px!important;),不传入则不设定最小宽度 | 可选项; //dragable:true, //指定是否要支持拖动效果,默认可以拖动 | 可选项; easing: false, //滚动是否需要滑动效果,默认有滑动效果 | 可选项; // handleCssAlter:"draghandlealter", //指定拖动鼠标时滚动条的样式,不传入该参数则没有变化效果 | 可选项; mousewheel: {bind: false, moveLength: 500} //mousewheel: bind->'true',绑定mousewheel事件; ->'false',不绑定mousewheel事件;moveLength是指定鼠标滚动一次移动的距离,默认值:{bind:true,moveLength:300} | 可选项; }); } } function bindParaCollapse($element) { $element.click(function () { var resizeTime = new Date(); var self = this; self.lastResizeTime = resizeTime; setTimeout(function () { if (resizeTime === self.lastResizeTime) { self.lastResizeTime = null; // REPORT-4620,这里使用delete在IE8以下会有bug paraContainer.animate({ height: (self.hasCollapsed ? "+=" : "-=") + (paraConfig.height + barHeight + paramstemplateHeight) }, "fast", function () { var height = paraContainer.height(); self.hasCollapsed = !self.hasCollapsed; $expandEl.switchClass('parameter-container-collapseimg-down', 'parameter-container-collapseimg-up'); borderLayout.getWidgetByName(alignLayoutName).setVisible(!self.hasCollapsed); if (borderLayout.getWidgetByName('paramsTemplatePane') !== undefined) { borderLayout.getWidgetByName('paramsTemplatePane').setVisible(!self.hasCollapsed); borderLayout.getWidgetByName('saveBtn').setVisible(!self.hasCollapsed); borderLayout.getWidgetByName('newParamsBtn').setVisible(!self.hasCollapsed); } borderLayout.regionField.center.el = $('.content-container'); borderLayout.setRegionWH('north', height); borderLayout.doLayout(); form.fireEvent(FR.Events.PARACOLLAPSE); if (FR.Form.bindFitResize && FR.Browser.isIE8Before()) { form.loadContentPane(true); } $('[widgetname^=sortable]').removeAttr('style'); }); } }, 222); }); } }) script> <script type="text/javascript"> script>

要抓取 Chrome 浏览器的数据,您需要安装 selenium 库,并且需要下载Chrome Web Driver.

下面是一个简单的 Python 代码片段,可获取Chrome浏览器中打开的所有标签页的标题并将其按字母顺序排序:

from selenium import webdriver

# 指定 Chrome Web Driver 的路径
driver = webdriver.Chrome('path/to/chromedriver')

# 打开 Chrome 浏览器并获取所有标签页的标题
driver.get('chrome://tabs/')
tab_titles = [tab.text for tab in driver.find_elements_by_css_selector('div.Title')]

# 按字母顺序排序标签页标题
sorted_tab_titles = sorted(tab_titles)

# 输出所有排过序的标签页标题
for title in sorted_tab_titles:
    print(title)

# 关闭 Chrome 浏览器
driver.quit()

在上述 Python 代码片段中,首先从 selenium 库导入 webdriver 类。然后,指定 Chrome Web Driver 的路径,并创建一个新的 webdriver 对象。

接下来,使用 get() 方法打开 Chrome 浏览器并获取所有标签页的标题。这是通过访问 Chrome 内部页面 chrome://tabs/ 并使用 CSS 选择器来获取所有标签页的标题完成的。find_elements_by_css_selector() 方法返回指定 CSS 选择器的所有元素,然后使用列表推导式将标题提取到一个列表中。

然后,使用 Python 的 sorted() 函数将标题列表按字母顺序排序,并将排序后的列表存储在另一个变量中。

最后,使用 for 循环输出所有按字母顺序排序的标签页标题,并使用 quit() 方法关闭 Chrome 浏览器。

需要注意的是,此代码是使用Chrome Web Driver的默认选项运行的,如果您需要使用不同的Chrome Web Driver选项,请根据您的需求修改代码。