pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<small id='j6Via'></small><noframes id='j6Via'>

  • <tfoot id='j6Via'></tfoot>

      <bdo id='j6Via'></bdo><ul id='j6Via'></ul>
  • <legend id='j6Via'><style id='j6Via'><dir id='j6Via'><q id='j6Via'></q></dir></style></legend>
    1. <i id='j6Via'><tr id='j6Via'><dt id='j6Via'><q id='j6Via'><span id='j6Via'><b id='j6Via'><form id='j6Via'><ins id='j6Via'></ins><ul id='j6Via'></ul><sub id='j6Via'></sub></form><legend id='j6Via'></legend><bdo id='j6Via'><pre id='j6Via'><center id='j6Via'></center></pre></bdo></b><th id='j6Via'></th></span></q></dt></tr></i><div class="xtd7hh5" id='j6Via'><tfoot id='j6Via'></tfoot><dl id='j6Via'><fieldset id='j6Via'></fieldset></dl></div>

        如何使用 XMLHttpRequest 在后臺下載 HTML 頁面并從中

        How to use XMLHttpRequest to download an HTML page in the background and extract a text element from it?(如何使用 XMLHttpRequest 在后臺下載 HTML 頁面并從中提取文本元素?)
        1. <legend id='W4lFL'><style id='W4lFL'><dir id='W4lFL'><q id='W4lFL'></q></dir></style></legend>
          <i id='W4lFL'><tr id='W4lFL'><dt id='W4lFL'><q id='W4lFL'><span id='W4lFL'><b id='W4lFL'><form id='W4lFL'><ins id='W4lFL'></ins><ul id='W4lFL'></ul><sub id='W4lFL'></sub></form><legend id='W4lFL'></legend><bdo id='W4lFL'><pre id='W4lFL'><center id='W4lFL'></center></pre></bdo></b><th id='W4lFL'></th></span></q></dt></tr></i><div class="fbppfbn" id='W4lFL'><tfoot id='W4lFL'></tfoot><dl id='W4lFL'><fieldset id='W4lFL'></fieldset></dl></div>

            <bdo id='W4lFL'></bdo><ul id='W4lFL'></ul>
              <tfoot id='W4lFL'></tfoot>

                  <tbody id='W4lFL'></tbody>

                • <small id='W4lFL'></small><noframes id='W4lFL'>

                  本文介紹了如何使用 XMLHttpRequest 在后臺下載 HTML 頁面并從中提取文本元素?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想制作一個 Greasemonkey 腳本,當您在 URL_1 中時,該腳本會在后臺解析 URL_2 的整個 HTML 網(wǎng)頁,以便從中提取文本元素.

                  I want to make a Greasemonkey script that, while you are in URL_1, the script parses the whole HTML web page of URL_2 in the background in order to extract a text element from it.

                  具體來說,我想在后臺下載整個頁面的HTML代碼(一個爛番茄頁面)并將其存儲在一個變量中,然后使用getElementsByClassName[0] 以便從類名為critic_consensus"的元素中提取我想要的文本.

                  To be specific, I want to download the whole page's HTML code (a Rotten Tomatoes page) in the background and store it in a variable and then use getElementsByClassName[0] in order to extract the text I want from the element with class name "critic_consensus".


                  我在 MDN 中找到了這個:XMLHttpRequest 中的 HTML所以,我最終得到了這個不幸的非工作代碼:


                  I've found this in MDN: HTML in XMLHttpRequest so, I ended up in this unfortunately non-working code:

                  var xhr = new XMLHttpRequest();
                  xhr.onload = function() {
                    alert(this.responseXML.getElementsByClassName(critic_consensus)[0].innerHTML);
                  }
                  xhr.open("GET", "http://www.rottentomatoes.com/m/godfather/",true);
                  xhr.responseType = "document";
                  xhr.send();
                  

                  當我在 Firefox Scratchpad 中運行它時,它會顯示此錯誤消息:

                  It shows this error message when I run it in Firefox Scratchpad:

                  跨域請求被阻止:同源策略不允許讀取http://www.rottentomatoes.com/m/godfather/ 的遠程資源.這可以通過將資源移動到同一域或啟用 CORS.

                  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.rottentomatoes.com/m/godfather/. This can be fixed by moving the resource to the same domain or enabling CORS.


                  PS.我不使用爛番茄 API 的原因是 他們已經(jīng)刪除了批評者的共識.

                  推薦答案

                  對于跨域請求,獲取的站點沒有幫助設(shè)置許可CORS 策略,Greasemonkey 提供 GM_xmlhttpRequest() 函數(shù).(大多數(shù)其他用戶腳本引擎也提供此功能.)

                  For cross-origin requests, where the fetched site has not helpfully set a permissive CORS policy, Greasemonkey provides the GM_xmlhttpRequest() function. (Most other userscript engines also provide this function.)

                  GM_xmlhttpRequest 明確設(shè)計為允許跨域請求.

                  GM_xmlhttpRequest is expressly designed to allow cross-origin requests.

                  要獲取您的目標信息,請在結(jié)果上創(chuàng)建一個 DOMParser.不要使用 jQuery 方法,因為這會導致加載無關(guān)的圖像、腳本和對象、減慢速度或使頁面崩潰.

                  To get your target information create a DOMParser on the result. Do not use jQuery methods as this will cause extraneous images, scripts and objects to load, slowing things down, or crashing the page.

                  這里有一個完整的腳本來說明這個過程:

                  Here's a complete script that illustrates the process:

                  // ==UserScript==
                  // @name        _Parse Ajax Response for specific nodes
                  // @include     http://stackoverflow.com/questions/*
                  // @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
                  // @grant       GM_xmlhttpRequest
                  // ==/UserScript==
                  
                  GM_xmlhttpRequest ( {
                      method: "GET",
                      url:    "http://www.rottentomatoes.com/m/godfather/",
                      onload: function (response) {
                          var parser  = new DOMParser ();
                          /* IMPORTANT!
                              1) For Chrome, see
                              https://developer.mozilla.org/en-US/docs/Web/API/DOMParser#DOMParser_HTML_extension_for_other_browsers
                              for a work-around.
                  
                              2) jQuery.parseHTML() and similar are bad because it causes images, etc., to be loaded.
                          */
                          var doc         = parser.parseFromString (response.responseText, "text/html");
                          var criticTxt   = doc.getElementsByClassName ("critic_consensus")[0].textContent;
                  
                          $("body").prepend ('<h1>' + criticTxt + '</h1>');
                      },
                      onerror: function (e) {
                          console.error ('**** error ', e);
                      },
                      onabort: function (e) {
                          console.error ('**** abort ', e);
                      },
                      ontimeout: function (e) {
                          console.error ('**** timeout ', e);
                      }
                  } );
                  

                  這篇關(guān)于如何使用 XMLHttpRequest 在后臺下載 HTML 頁面并從中提取文本元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                • <tfoot id='sFPV3'></tfoot>

                • <legend id='sFPV3'><style id='sFPV3'><dir id='sFPV3'><q id='sFPV3'></q></dir></style></legend>

                        <bdo id='sFPV3'></bdo><ul id='sFPV3'></ul>
                          <tbody id='sFPV3'></tbody>

                            <small id='sFPV3'></small><noframes id='sFPV3'>

                            <i id='sFPV3'><tr id='sFPV3'><dt id='sFPV3'><q id='sFPV3'><span id='sFPV3'><b id='sFPV3'><form id='sFPV3'><ins id='sFPV3'></ins><ul id='sFPV3'></ul><sub id='sFPV3'></sub></form><legend id='sFPV3'></legend><bdo id='sFPV3'><pre id='sFPV3'><center id='sFPV3'></center></pre></bdo></b><th id='sFPV3'></th></span></q></dt></tr></i><div class="lpbjnj5" id='sFPV3'><tfoot id='sFPV3'></tfoot><dl id='sFPV3'><fieldset id='sFPV3'></fieldset></dl></div>
                            主站蜘蛛池模板: 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 广州展台特装搭建商|特装展位设计搭建|展会特装搭建|特装展台制作设计|展览特装公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 气胀轴|气涨轴|安全夹头|安全卡盘|伺服纠偏系统厂家-天机传动 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 线材成型机,线材折弯机,线材成型机厂家,贝朗自动化设备有限公司1 | 威廉希尔WilliamHill·足球(中国)体育官方网站 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 牛奶检测仪-乳成分分析仪-北京海谊 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 小学教案模板_中学教师优秀教案_高中教学设计模板_教育巴巴 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程| 体坛网_体坛+_体坛周报新闻客户端 | 1000帧高速摄像机|工业高速相机厂家|科天健光电技术 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 工控机-工业平板电脑-研华工控机-研越无风扇嵌入式box工控机 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 | 分类168信息网 - 分类信息网 免费发布与查询 | 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 米顿罗计量泵(科普)——韬铭机械 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | ★塑料拖链__工程拖链__电缆拖链__钢制拖链 - 【上海闵彬】 | 巨野月嫂-家政公司-巨野县红墙安康母婴护理中心 | 希望影视-高清影视vip热播电影电视剧免费在线抢先看 | 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 |