如何解析URL的HTML并使用Google脚本将数据插入Google表格?

时间:2018-12-08 17:36:34

标签: javascript google-apps-script google-sheets

我对Google Apps脚本和JavaScript完全陌生。

我只是想:

  1. 阅读 A栏,并进行URL请求。
  2. 解析URL的HTML以获得titlenum count
  3. title写入 B列,并将num count写入 C列

这是我的代码:

function getHtml()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var data = sheet.getDataRange().getValues();
    var url_range = sheet.getRange('A1:A').getValue();
    var response = UrlFetchApp.fetch(url);
    var content = response.getContentText("UTF-8"); 

    var title = new Array();
    var num_count = new Array();

    for (var i = 1; i < data.length; i++) {
        title = content.match(/<title>(.*?)<\/title>/);
        num_count = content.match(/<span class="num_count">(.*?)<\/span>/);
    }

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("sheet1");

    for (var i = 1; i < data.length; i++) {
        sheet1.getRange(i,2).setValue(title[i]);
        sheet1.getRange(i,3).setValue(num_count[i]);
    }
}

但是我只能得到以下结果:

enter image description here

请告诉我我应该改进什么。

1 个答案:

答案 0 :(得分:0)

假设您的抓取有效,我认为这与您的要求非常接近。

from tkinter import *
import sys

class Window():
    def __init__(self, master, x, y, title):
        self.master = master
        master.title(title)
        master.geometry("{}x{}".format(x,y))

    def new_window(self, class_name):
        self.master.destroy()
        class_name = getattr(sys.modules[__name__], str(class_name))
        root=Tk()
        class_name(root)
        root.mainloop()

class Login(Window):
    def __init__(self, master):
        super().__init__(master, 200, 200, "Login")
        Button(master, text="NEXT",width=7,height=1,command=self.new_window("Home")).grid(row=1,column=1)

class Home(Window):
    def __init__(self, master):
        super().__init__(master, 200, 200, "Home")
        Button(master, text="NEXT",width=7,height=1,command=self.new_window("Login")).grid(row=1,column=1)

def main():
    root=Tk()
    Login(root)
    root.mainloop()

if __name__ == '__main__':
    main()