【发布时间】:2023-01-15 02:28:01
【问题描述】:
首先我需要说明我不是程序员,但是,我试图成为一个理性的人并且我知道基础知识,但是我遇到的问题对我来说太难了。
我请求允许解析其所有者的站点,我需要从中获取少量表格数据。我收到了。
html 站点的结构如下所示:
<html>
<head>
<title>TITLE AAAAA</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="IE=EmulateIE7" http-equiv="X-UA-Compatible">
<style type="text/css">
body {
font-size: 12px;
font-family: Arial
}
td {
font-size: 12px;
line-height: 20px;
font-family: Arial
}
</style>
<script type="text/javascript" language="javascript" src="Function.js"></script>
</head>
<body>
<p align="center">
<b>AAAA: AAAAAA</b>
</p>
<table width="300" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#0066cc">
<tbody>
<tr align="center" bgcolor="#333399" class="font13">
<td width="150">
<b>
<font color="#ffffff">TO_CELL_A1_TEXT</font>
</b>
</td>
<td width="150">
<b>
<font color="#ffffff">TO_CELL_B1_TEXT</font>
</b>
</td>
<td width="150">
<b>
<font color="#ffffff">TO_CELL_C1_TEXT</font>
</b>
</td>
<td width="150">
<b>
<font color="#ffffff">TO_CELL_D1_TIME_TEXT</font>
</b>
</td>
</tr>
<tr align="center" bgcolor="#FFFFFF">
<td height="20">
<b>
<font color="red">TO_CELL_A2_TEXT</font>
</b>
</td>
<td>
<b>
<font color="red">TO_CELL_B2_TEXT</font>
</b>
</td>
<td>
<b>
<font color="red">TO_CELL_C2_TEXT</font>
</b>
</td>
<td>
<script>
showtime(2023, 01 - 1, 13, 23, 01, 12)
</script>"TO_CELL_D2_TIME_TEXT"
</td>
</tr>
</tbody>
</table>
<br>
<p align="center">SITE_NAME</p>
</body>
</html>
我需要将这些数据解析到我的电子表格中,但我遇到了很多问题,我开始抓狂。 我停在这个脚本代码处:
function addMenuTab() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var newTab = sheet.insertSheet("IMPORTED_DATA");
var menu = SpreadsheetApp.getUi().createMenu("MY SCRIPT")
.addItem("IMPORT DATA", "importParsedData")
.addToUi();
}
function importParsedData(){
var html = UrlFetchApp.fetch('http://sitename.com').getContentText();
html = html.replace(/(<(?=link|meta|br)[^>]*)(?<!\/)>/ig, '$1/>')
html = html.replace(/&(?!amp;)/ig, '&')
html = html.replace(/ /g, " ")
html = html.replace(/<table[^>]*>/ig, "<table>")
html = html.replace(/<tr[^>]*>/ig, "<tr>")
html = html.replace(/<td[^>]*>/ig, "<td>")
html = html.replace(/<font[^>]*>/ig, "<font>")
html = html.replace(/width="([^"])"/g, "width='$1'")
html = html.replace(/height="([^"])"/g, "height='$1'")
//html = html.replace(/(<(script|style)[^>]*>)/ig, '$1<![CDATA[').replace(/(<\/(script|style)[^>]*>)/ig, ']]>$1')
//html = html.replace(/width="[^"]*"/g, "")
//html = html.replace(/<table[^>]width="[^"]"[^>]>/ig, "<table width=''>")
//html = html.replace(/<td[^>]width="[^"]"[^>]>/ig, "<td width=''>")
//html = html.replace(/<td[^>]height="[^"]"[^>]>/ig, "<td height=''>")
var doc = XmlService.parse(html);
var tables = doc.getAttribute("table");
if (tables.length < 1) return;
var table = tables[0];
var tbody = table.getAttribute("tbody");
var rows = tbody[0].getAttribute("tr");
var sheet = SpreadsheetApp.getActive().getSheetByName("IMPORTED_DATA");
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].getAttribute("td");
for (var j = 0; j < cells.length; j++) {
var cellValue = cells[j].getValue("td");
sheet.getRange(i + 1, j + 1).setValue(cellValue);
}
}
}
我想我已经把一切都搞混了。最有可能的是,我错误地将数据作为标签或属性拾取,而且,我可能已经清除了太多,包括我想要在标签之间获取到我的工作表的值。我知道还有jsoup之类的其他解析方式,但是对于我这样的菜鸟来说太难了。 你能帮我处理我的代码吗?
【问题讨论】:
-
你好,谢谢你的回答。正如您在网站源代码中看到的那样,有 <script></scirpt> 和数据形式,对于 D1:D 列的值,我想重新获得。当我使用公式 =importhtml("sitename.com";"table";1;"en_US") 时,我收到空的 D 列。无论如何,在我遇到 <script> 问题之前,我一直停留在简单的问题上。我想根据遇到的示例和问题学习如何编写脚本. 但是,我的大脑上次吸收了太多信息,我无法在我自己的atm上处理这个问题,所以我想继续前进,并在你的帮助下分析我的错误。
-
欢迎Stack Overflow。这个问题不适合这个网站。首先你应该删除“聊天”(所有关于你自己的东西都不应该包含在问题或答案中,如果分享对你来说很重要,你可以将它包含在你的个人资料中)。当寻求有关修复代码的帮助时,问题应包括minimal reproducible example,这意味着除了页面结构和代码之外,您还应包括文本错误消息以及其他人重现错误所需的任何其他相关详细信息。
-
此外,您应该简要说明您为从该站点找到有用的内容而进行的搜索工作,并解释为什么最相关的帖子对您没有帮助。
-
由于问题标题和代码,您似乎使用了非常古老的资源。搜索此站点以获取有关使用 google apps 脚本进行网络抓取的问题。这可能会给你一些提示:stackoverflow.com/q/69350317/1595451
标签: javascript html regex google-apps-script web-scraping