第一種方法:這裏是你如何能做到這(使用IMPORTXML和REGEXEXTRACT:
=IMPORTXML("http://www.immopreise.at/Wien/Wohnung/Miete",
"//table[@id='preisTabelle']")
所提到的代碼產生一個空字符串,因爲網頁上有一個空表在該位置,如下圖所示:
<table id="preisTabelle"></table>
的數據實際上是位於一個<script>
標記內:
<script>
var ImmoOptions = {"mapOptions":{"region":"Wien","karteAnzeigen":true},"TrendChartConf":{"uri":{"district":"/Trend/GetDistricts","chart":"/Trend/GetChart","chart1":"/Trend/GetTrendChart","compare":"/Preisvergleich","chart2":"/Trend/TrendChart","refresh":"/Preisentwicklung","uebersicht":"/?region=Wien\u0026pathInfo=Wohnung%2FMiete"},"firstDirstrict":{"Wien":"Wien-1-Innere-Stadt","Niederoesterreich":"Sankt-Poelten-Stadt","Burgenland":"Eisenstadt-Stadt","Oberoesterreich":"Linz-Stadt","Steiermark":"Graz-Alle-Bezirke","Kaernten":"Klagenfurt-Stadt","Salzburg":"Salzburg-Stadt","Tirol":"Innsbruck-Stadt","Vorarlberg":"Bregenz"},"firstDirstrictId":{"9":231,"3":153,"1":133,"4":177,"6":201,"2":142,"5":195,"7":218,"8":228}},"preisInfos":{"tabelle":{"spalten":[{"name":"≤50m²","spaltenArt":"Waehrung","nachkommaStellen":true,"farbmarkierung":null},{"name":"51-80m²","spaltenArt":"Waehrung","nachkommaStellen":true,"farbmarkierung":null},{"name":"81-129m²","spaltenArt":"Waehrung","nachkommaStellen":true,"farbmarkierung":null},{"name":"\u003e130m²","spaltenArt":"Waehrung","nachkommaStellen":true,"farbmarkierung":null},{"name":"\u003cspan class=\u0027Detailed\u0027\u003e\u0026#216;/m²\u003c/span\u003e\u003cspan class=\u0027Compact\u0027\u003e\u0026#216;/m²\u003c/span\u003e","spaltenArt":"Waehrung","nachkommaStellen":true,"farbmarkierung":true},{"name":"\u003cspan class=\u0027Detailed\u0027\u003eTrend\u003c/span\u003e\u003cspan class=\u0027Compact\u0027\u003eTd.\u003c/span\u003e","spaltenArt":"Tendenz","nachkommaStellen":false,"farbmarkierung":null}],"zeilen":[{"name":" 1., Innere Stadt","zellen":[21.68,19.02,18.43,19.56,19.27,0],"id":231},{"name":" 2., Leopoldstadt","zellen":[18.27,15.06,14.28,14.20,14.85,1],"id":232},{"name":" 3., Landstraße","zellen":[18.88,17.04,15.42,14.68,16.03,1],"id":233},{"name":" 4., Wieden","zellen":[19.37,16.58,16.89,16.35,16.83,1],"id":234},{"name":" 5., Margareten","zellen":[15.46,14.11,14.20,14.77,14.39,0],"id":235},{"name":" 6., Mariahilf","zellen":[18.23,14.68,15.72,15.32,15.53,1],"id":236},{"name":" 7., Neubau","zellen":[16.09,14.89,14.58,14.94,14.95,0],"id":237},{"name":" 8., Josefstadt","zellen":[16.77,16.78,14.02,14.93,15.08,0],"id":238},{"name":" 9., Alsergrund","zellen":[15.72,14.48,14.53,14.92,14.69,0],"id":239},{"name":"10., Favoriten","zellen":[14.14,12.35,11.81,0,12.52,0],"id":240},{"name":"11., Simmering","zellen":[13.69,12.34,11.50,13.46,12.38,-1],"id":241},{"name":"12., Meidling","zellen":[15.66,14.97,13.28,11.79,14.54,1],"id":242},{"name":"13., Hietzing","zellen":[16.71,15.93,14.63,14.05,14.99,0],"id":243},{"name":"14., Penzing","zellen":[14.43,13.14,12.72,12.37,13.11,0],"id":244},{"name":"15., Rudolfsheim-Fünfhaus","zellen":[13.58,12.90,12.93,11.76,13.07,0],"id":245},{"name":"16., Ottakring","zellen":[13.99,12.64,12.64,12.45,12.90,0],"id":246},{"name":"17., Hernals","zellen":[14.71,13.06,13.15,13.61,13.50,1],"id":247},{"name":"18., Währing","zellen":[14.47,13.96,13.82,15.67,14.37,0],"id":248},{"name":"19., Döbling","zellen":[16.21,14.37,15.06,16.44,15.29,0],"id":249},{"name":"20., Brigittenau","zellen":[15.68,13.57,12.56,13.30,13.43,0],"id":250},{"name":"21., Floridsdorf","zellen":[15.58,13.58,12.38,14.97,13.68,1],"id":251},{"name":"22., Donaustadt","zellen":[18.19,15.57,16.85,15.89,16.18,0],"id":252},{"name":"23., Liesing","zellen":[14.79,14.09,13.49,15.60,13.92,1],"id":253}],"tabellenTitel":"Wohnungen Miete","titelErsteSpalte":"Bezirk","GesamtAnzahlObjekte":12739},"preisspannen":[{"bis":12},{"bis":14},{"bis":15},{"bis":null}]},"basecharts":null,"CurrentView":{"trendVar":{"CatNum":0,"ImmoArtNum":5,"AltbauNum":2,"AngebotTypeNum":1},"hid":0}} ;
jQuery(function() {
InitMap(ImmoOptions.mapOptions);
});
</script>
最感興趣的數據中發現變量ImmoOptions內:
[
{
"name": " 1., Innere Stadt",
"zellen": [21.68, 19.02, 18.43, 19.56, 19.27, 0],
"id": 231
},
{
"name": " 2., Leopoldstadt",
"zellen": [18.27, 15.06, 14.28, 14.2, 14.85, 1],
"id": 232
},
/* Edited for brevity */
]
下面的公式可以拿到劇本到電子表格單元格(假設我們粘貼成電池A [100])..
=IMPORTXML("http://www.immopreise.at/Wien/Wohnung/Miete","//script[2]")
然後,下式提取JSON字符串(ImmoOptions變量的值)插入細胞(假設我們粘貼以下到小區A [1])..
=REGEXEXTRACT(A100,"(?s)=(.*)")
至此,我們需要javascript來解析JSON。這可以通過將工作表轉換爲Google App(工具 - >腳本編輯器)並使用JavaScript進行編碼來完成。
在JavaScript中,將有三個步驟(細節這裏沒有顯示):
1. Use IMPORTXML to get the data inside script (in the url/page)
2. Use REGEXEXTRACT to get the value of ImmoOptions as JSON string
3. Parse JSON string to get the data
方法二:這裏是如何使用谷歌應用程序/腳本做到這一點:
登錄谷歌並在瀏覽器中打開this spreadsheet。
選擇File->Make a Copy
(可能帶有像S1這樣的名稱)。這將在您的谷歌驅動器中製作該文件的副本;並在新標籤中打開它。
轉到該新窗口/選項卡。選擇Tools->Script Editor
。這會讓你和腳本一起編輯。從工具欄中選擇功能doGet
並運行該腳本;它會生成電子表格。
這裏(鏈接去錯過參考,萬一)附有片的腳本:
function doGet() {
var r1=Math.random()*100000000000;
var html = UrlFetchApp.fetch("http://www.immopreise.at/Wien/Wohnung/Miete?somevariable=" + r1).getContentText();
var re = /var ImmoOptions = (.*);/i;
var jo=JSON.parse(re.exec(html)[1]);
var arr=jo["preisInfos"]["tabelle"]["zeilen"];
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear(); sheet.appendRow([r1]);
sheet.appendRow(['Bezirk','Col-1','Col-2','Col-3','Col-4','Col-5','Col-6']);
for (var i=0;i<arr.length;i++) {
var item = arr[i]; var row=[item.name];
row=row.concat(item.zellen); sheet.appendRow(row);
}
}
它是如何工作的:
- 再換相關網址的完整html內容。
- 使用正則表達式從內部提取json數據
<script>..</script>
- 解析提取的json數據。
- 獲取相關數組;填充到電子表格中。
缺點:
- 這是一種脆性補丁工作腳本,將與
<script>
變化包括(或以任何其它方式正則表達式的休息時間)
- 不給你破桌面用戶界面的好控件(可以構建它們,但有更多的工作)。
- 僅適用於整個json數據位於一行(可以通過刪除新行或使用正確的正則表達式進行修改)。
Thx爲您的答覆!任何建議如何解析JSON字符串到表格式? – mrquad
我想把它變成谷歌應用程序。我有json數據。一旦我能夠將其放入電子表格中,我將發佈我的答案。 – blackpen
@mrquad,將兩個答案合併成一個(第一個答案爲半自動化;第二個答案使用Google Apps腳本進行全自動化)。 – blackpen