通过iBot的python脚本,把html的table数据转换为json的脚本。

leoliu 2020-12-21 · 8个月前
2 198

通过iBot的python脚本,把html的table数据转换为json的脚本。

1. 如下脚本复制到脚本节点中

2. {176}替换为获取到的"通过innerHTML的方式获取到的html table的html"


代码:

# import html

# 用于转换无嵌套的html table为json二维数组

def main():
    content = '''{176}''' # 176是通过innerHTML的方式获取到的html table的html
    trCount = content.lower().count('</tr>')
    trIndex = 0
    contentList = []
    for trNo in range(trCount):
        trCharStartIndex = content.lower().find('<tr', trIndex)
        trCharEndIndex = content[trCharStartIndex:].lower().find('</tr>')
        trStrContent = content[trCharStartIndex:trCharStartIndex + trCharEndIndex]
        tdCount = trStrContent.lower().count('</td>')
        trIndex = trCharStartIndex + trCharEndIndex
        tdIndex = 0
        row = []
        for tdNo in range(tdCount):
            index2 = 0;
            index3 = 0;
            tdStartIndex = trStrContent.lower().find('<td', tdIndex)
            index2 = trStrContent.find('>', tdStartIndex + 3) + 1
            index3 = trStrContent.lower().find('</td>', index2)
            cellStr = trStrContent[index2:index3]
            # cellStr = html.unescape(cellStr) 
            row.append(cellStr)
            tdIndex = index3 + 5
        contentList.append(row)
    return contentList

论坛帖子(共 2 条评论)

leoliu [楼主] 8个月前 0

如上176变量对应的html字符串的样例:

<THEAD>

<TR class=row_odd>
<TH id=col_X_TAG style="BORDER-LEFT-WIDTH: 0px"> </TH><!--add by liuhl7 qc:33854-->
<TH id=col_SORT_NUMBER style="BORDER-LEFT-WIDTH: 0px">序号</TH><!--end liuhl7 qc:33854-->
<TH id=col_CUST_ID style="BORDER-LEFT-WIDTH: 0px">客户编码</TH>
<TH id=col_USER_ID>用户标识</TH>
<TH id=col_BRAND_CODE style="DISPLAY: none">用户品牌</TH>
<TH id=col_BRAND>用户品牌</TH>
<TH id=col_SERIAL_NUMBER>业务号码</TH>
<TH id=col_PRODUCT_ID>产品编码</TH>
<TH id=col_PRODUCT_NAME>产品名称</TH>
<TH id=col_OPEN_DATE>开户日期</TH>
<TH id=col_X_REMOVE_TAG_NAME style="DISPLAY: none">用户在网状态</TH><!--TFS:179282 -->
<TH id=col_USER_STATE>当前使用状态</TH><!--TFS:179282 -->
<TH id=col_FEE_LEFT>用户余额</TH>
<TH id=col_NET_TYPE_CODE style="DISPLAY: none">网别</TH>
<TH id=col_REMOVE_TAG style="DISPLAY: none">状态</TH>
<TH id=col_IS_NEW style="DISPLAY: none">是否为新系统号码</TH><!-- qc 7587 begin -->
<TH id=col_USER_STATE style="DISPLAY: none">服务状态</TH><!-- qc 7587 end -->
<TH id=col_USECUST_ID style="DISPLAY: none">在用客户ID</TH></TR></THEAD>
<TBODY>
<TR class=row_even>
<TD>bAeD=1&b=2</TD>
<TD>1</TD>
<TD>7520122050084060</TD>
<TD>7520122162053556</TD>
<TD style="DISPLAY: none">GZKD</TD>
<TD>公众宽带</TD>
<TD>079509104050</TD>
<TD>90568971</TD>
<TD>江西融合宽带100M0元/月</TD>
<TD>2020-12-21 15:27:27</TD>
<TD style="DISPLAY: none">正常</TD>
<TD>开通</TD>
<TD>0.0</TD>
<TD style="DISPLAY: none">40</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">开通</TD>
<TD style="DISPLAY: none">7520122050084060</TD></TR>
<TR class=row_select>
<TD></TD>
<TD>1</TD>
<TD>7520122050084060</TD>
<TD>7520122061984499</TD>
<TD style="DISPLAY: none">4G00</TD>
<TD>沃4G</TD>
<TD>13026292819</TD>
<TD>90692900</TD>
<TD>江西全国流量王59(放心用版)</TD>
<TD>2020-12-20 11:38:30</TD>
<TD style="DISPLAY: none">正常</TD>
<TD>开通</TD>
<TD>93.32</TD>
<TD style="DISPLAY: none">50</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">开通</TD>
<TD style="DISPLAY: none">7520122050084060</TD></TR>
<TR class=row_even>
<TD></TD>
<TD>1</TD>
<TD>7520122050084060</TD>
<TD>7520122162053553</TD>
<TD style="DISPLAY: none">COMP</TD>
<TD>组合产品</TD>
<TD>0795XN2162053553</TD>
<TD>90694786</TD>
<TD>江西智慧沃家组合59元版 </TD>
<TD>2020-12-21 15:26:57</TD>
<TD style="DISPLAY: none">正常</TD>
<TD>开通</TD>
<TD>0.0</TD>
<TD style="DISPLAY: none">CP</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">0</TD>
<TD style="DISPLAY: none">开通</TD>
<TD style="DISPLAY: none">7520122050084060</TD></TR></TBODY>

leoliu [楼主] 8个月前 0

如果需要处理html的转移符,则在iBot脚本中选择Python3,并取消如下的#注释行即可。


参考: html转义符: https://tool.oschina.net/commons?type=2

  • 最新留言
  • 最近热帖
  • 最热词云

属于iBot的bbs

在这里,您可以提问,回答,分享,诉说,这是个属于所有iBot用户的社区,欢迎您的加入!