C# HTML 읽고 파싱

C#_.NET 2017. 8. 18. 20:45







웹데이터를 importhtml로 읽어 파싱하는 법..


C#으로 읽고 파싱하기


the best way of converting a Html table to datatable

var doc = new HtmlDocument();
doc.Load(url);

var nodes = doc.DocumentNode.SelectNodes("//table/tr");
var table = new DataTable("MyTable");

var headers = nodes[0]
    .Elements("th")
    .Select(th => th.InnerText.Trim());
foreach (var header in headers)
{
    table.Columns.Add(header);
}

var rows = nodes.Skip(1).Select(tr => tr
    .Elements("td")
    .Select(td => td.InnerText.Trim())
    .ToArray());
foreach (var row in rows)
{
    table.Rows.Add(row);
}


https://stackoverflow.com/questions/6261697/what-is-the-best-way-of-converting-a-html-table-to-datatable

시트 데이터 복사
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// StockShareTrace = _sSheet,    대상 종목시트 = _tSheet
// --------------------------------------------------------------------------
function CopySheet(_sSheet,_tSheet){           
  var sRange = _sSheet.getDataRange();         // Get full range of data
  var sourceRange = _sSheet.getRange(1, 1, _sSheet.getLastRow(), _sSheet.getLastColumn()); 
  var targetRange = _tSheet.getRange(1, 1, 1, 1); 
  sourceRange.copyTo(targetRange);
};
// --------------------------------------------------------------------------
// https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow-column-numrows-numcolumns
// var range = sheet.getRange(1, 1, 3, 3);
// https://stackoverflow.com/questions/16090447/paste-special-values-google-apps-script
// --------------------------------------------------------------------------
function CopyRange(source,destination) {
  var sourceSheet = source.getSheet();
  var destSheet = destination.getSheet();
  var sourceData = source.getValues();
  var dest = destSheet.getRange(
    destination.getRow(),        // Top row of destination
    destination.getColumn(),     // left col of destination
    sourceData.length,           // # rows in source
    sourceData[0].length);       // # cols in source (elements in first row)
  dest.setValues(sourceData);
  //source.clear();
};
// --------------------------------------------------------------------------
// https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow-column-numrows-numcolumns
// http://stackoverflow.com/questions/11058019/delete-a-row-in-google-spreadsheets-if-value-of-cell-in-said-row-is-0-or-blank
// http://stackoverflow.com/questions/36116050/delete-row-in-google-sheets-if-certain-word-is-found-in-cell
/**
 * Deletes rows in the active spreadsheet that contain 0 or
 * a blank valuein column "C". 
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
// Maximum run time of Google script per user or per account
// https://stackoverflow.com/questions/36804160/maximum-run-time-of-google-script-per-user-or-per-account
// https://www.google.co.kr/search?newwindow=1&q=google+script+exceeded+maximum+execution+time&sa=X&ved=0ahUKEwipj6XhrtHVAhVDk5QKHbOFCGMQ1QIIaCgD&biw=1137&bih=929
// "google script exceeded maximum execution time"
// 1 hour toal/day, 6 min/run
// The maximum allowed time for your script to run continuously is 6 minutes. 
// If it exceeds that limit, GAS would throw the "Exceeded maximum execution time" exception.
// --------------------------------------------------------------------------
function DeleteRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  var rowsDeleted = 0;
  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[2] == 0 || row[2] == '') {
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
};
// --------------------------------------------------------------------------
function test_moveRange() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  var destSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
  var source = sourceSheet.getRange("A7:C10");
  var destination = destSheet.getRange("C4:H2");
  moveRange(source,destination);
};
블로그 이미지

DIYworld

,