var str = '<table><tr><td>书名</td><td>姓名</td><td>借阅情况</td><td>借阅情况</td></tr>'; for (var i = 0; i < result.rows.length; i++) { item = result.rows.item(i); str += "<tr><td>" + item["book"] + "</td><td>" + item["author"] +"</td><td>" + item["borrow"] + "</td><td><input type='button'id='" +item["id"]+ "'class='borrowItem'value='借阅'></td></tr>"; } str += "</table>";
时间: 2023-08-14 10:35:11 浏览: 100
这段代码是用 JavaScript 语言编写的,主要功能是将从数据库中获取的数据渲染成一个表格,并在每行数据末尾添加一个“借阅”按钮。其中,变量 str 存储了渲染后的 HTML 代码,通过循环遍历 result.rows 数组,将每行数据的书名、姓名和借阅情况添加到表格中。最后,将 str 变量末尾的字符串 "</table>" 添加到表格代码的最后,完成表格的生成。
相关问题
import retext = """<table class="table table-bordered table-condensed"> <thead> <tr> <th>期号</th> <th colspan="11">开奖号码</th> <th>总和</th> </tr> </thead> <tbody> <tr class="new"> <td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>0</td> </tr> <tr class="new"> <td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td> </tr> <tr> <td colspan="2">正确次数</td> <td>4</td> <td>3</td> <td>7</td> <td>2</td> <td>9</td> <td>3</td> <td>4</td> <td>5</td> <td>3</td> <td>6</td> <td></td> </tr> <tr> <td colspan="2">错误次数</td> <td>16</td> <td>17</td> <td>13</td> <td>18</td> <td>11</td> <td>17</td> <td>16</td> <td>15</td> <td>17</td> <td>14</td> <td></td> </tr> <tr> <td colspan="2">当前连对</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td></td> </tr> <tr> <td colspan="2">当前连错</td> <td>1</td> <td>4</td> <td>2</td> <td>4</td> <td>1</td> <td>7</td> <td>1</td> <td>2</td> <td>13</td> <td>3</td> <td></td> </tr> <tr> <td colspan="2">最大连对</td> <td>2</td> <td>1</td> <td>3</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td></td> </tr> <tr> <td colspan="2">最大连错</td> <td>9</td> <td>7</td> <td>4</td> <td>12</td> <td>3</td> <td>7</td> <td>7</td> <td>4</td> <td>13</td> <td>3</td> <td></td> </tr> </table> </div> </div> </div> <script type="text/javascript" src="https://img.78500.cn/mobile/iscroll.js"></script> <script type="text/javascript"> var wrapper; function loaded() { wrapper = new iScroll("wrapper", { vScrollbar: false, hScrollbar: false, zoom: true, zoomMin: 0.5, zoomMax: 1 }); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); document.addEventListener('DOMContentLoaded', loaded, false); </script></body> </html>"""pattern = re.compile(r'<tr class="new">(.*?)</tr>', re.S)matches = pattern.findall(text)for match in matches: print(match) 没有匹配上 且没有匹配数字
可能是因为你的正则表达式中只匹配了包含`class="new"`的`<tr>`标签,而在这个HTML代码片段中只有一个`<tr class="new">`标签。你可以尝试修改正则表达式,匹配所有的`<tr>`标签,例如:
```
pattern = re.compile(r'<tr.*?>(.*?)</tr>', re.S)
```
这样就可以匹配所有的`<tr>`标签了。如果想要匹配数字,可以再使用正则表达式提取出数字部分,例如:
```
matches = pattern.findall(text)
for match in matches:
numbers = re.findall(r'\d+', match)
print(numbers)
```
这样就可以提取出所有的数字了。
<td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td >0</td> </tr> <tr class="new"> <td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td> </tr> <tr> <td colspan="2">正确次数</td> <td>4</td> <td>3</td> <td>7</td> <td>2</td> <td>9</td> <td>3</td> <td>4</td> <td>5</td> <td>3</td> <td>6</td> <td></td> </tr> <tr> <td colspan="2">错误次数</td> <td>16</td> <td>17</td> <td>13</td> <td>18</td> <td>11</td> <td>17</td> <td>16</td> <td>15</td> <td>17</td> <td>14</td> <td></td> </tr> <tr> <td colspan="2">当前连对</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td></td> </tr> <tr> <td colspan="2">当前连错</td> <td>1</td> <td>4</td> <td>2</td> <td>4</td> <td>1</td> <td>7</td> <td>1</td> <td>2</td> <td>13</td> <td>3</td> <td></td> </tr> <tr> <td colspan="2">最大连对</td> <td>2</td> <td>1</td> <td>3</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td></td> </tr> <tr> <td colspan="2">最大连错</td> <td>9</td> <td>7</td> <td>4</td> <td>12</td> <td>3</td> <td>7</td> <td>7</td> <td>4</td> <td>13</td> <td>3</td> <td></td> </tr> </table> </div> </div> </div> <script type="text/javascript" src="https://img.78500.cn/mobile/iscroll.js"></script> <script type="text/javascript"> var wrapper; function loaded() { wrapper = new iScroll("wrapper", { vScrollbar: false, hScrollbar: false, zoom: true, zoomMin: 0.5, zoomMax: 1 }); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); document.addEventListener('DOMContentLoaded', loaded, false); </script></body> </html> 对应上代码用正则表达式提取 以 <tr class="new"> 开头 </tr>结束 里面的数字
可以使用以下正则表达式进行提取:
```python
import re
text = """<table class="table table-bordered table-condensed"> <thead> <tr> <th>期号</th> <th colspan="11">开奖号码</th> <th>总和</th> </tr> </thead> <tbody> <tr class="new"> <td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>0</td> </tr> <tr class="new"> <td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td> </tr> <tr> <td colspan="2">正确次数</td> <td>4</td> <td>3</td> <td>7</td> <td>2</td> <td>9</td> <td>3</td> <td>4</td> <td>5</td> <td>3</td> <td>6</td> <td></td> </tr> <tr> <td colspan="2">错误次数</td> <td>16</td> <td>17</td> <td>13</td> <td>18</td> <td>11</td> <td>17</td> <td>16</td> <td>15</td> <td>17</td> <td>14</td> <td></td> </tr> <tr> <td colspan="2">当前连对</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td></td> </tr> <tr> <td colspan="2">当前连错</td> <td>1</td> <td>4</td> <td>2</td> <td>4</td> <td>1</td> <td>7</td> <td>1</td> <td>2</td> <td>13</td> <td>3</td> <td></td> </tr> <tr> <td colspan="2">最大连对</td> <td>2</td> <td>1</td> <td>3</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td></td> </tr> <tr> <td colspan="2">最大连错</td> <td>9</td> <td>7</td> <td>4</td> <td>12</td> <td>3</td> <td>7</td> <td>7</td> <td>4</td> <td>13</td> <td>3</td> <td></td> </tr> </table> </div> </div> </div> <script type="text/javascript" src="https://img.78500.cn/mobile/iscroll.js"></script> <script type="text/javascript"> var wrapper; function loaded() { wrapper = new iScroll("wrapper", { vScrollbar: false, hScrollbar: false, zoom: true, zoomMin: 0.5, zoomMax: 1 }); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); document.addEventListener('DOMContentLoaded', loaded, false); </script></body> </html>"""
pattern = re.compile(r'<tr class="new">(.*?)</tr>', re.S)
matches = pattern.findall(text)
for match in matches:
print(match)
```
这将输出以下内容:
```
<td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>0</td>
<td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td>
```
阅读全文