<div><table><tbody><tr><td>1</td><td>2</td></tr></tbody></table><tr><td>11</td><td>21</td></tr></div> 打印出<table>里的td的元素
时间: 2024-03-05 07:45:33 浏览: 84
以下是打印出<table>里的<td>元素的代码示例:
```python
import re
html = """<div><table><tbody><tr><td>1</td><td>2</td></tr></tbody></table><tr><td>11</td><td>21</td></tr></div>"""
td_elements = re.findall(r'<td>(.*?)</td>', html)
for element in td_elements:
print(element)
```
输出结果为:
```
1
2
11
21
```
相关问题
<div><table><tbody><tr><td>1</td><td>2</td></tr></tbody></table></div> 打印出td里的元素
以下是使用Python的BeautifulSoup解析HTML并打印出td元的代码示例:
```python
from bs4 import BeautifulSoup
html = "<div><table><tbody><tr><td>1</td><td>2</td></tr></tbody></table></div>"
soup = BeautifulSoup(html, 'html.parser')
td_elements = soup.find_all('td')
for td in td_elements:
print(td.text)
```
输出结果为:
```
1
2
```
<div><table><tbody><tr><td>1</td><td>2</td></tr></tbody></table><tr><td>11</td><td>21</td></tr></div> 打印出<tbody>里的td的元素
以下是打印出`<tbody>`里的`<td>`元素的示例:
```python
import re
html = "<div><table><tbody><tr><td>1</td><td>2</td></tr></></table><tr><td>11</td><td>21</td></tr></div>"
# 使用正则表达式匹配<td>标签里的内容
td_elements = re.findall(r"<td>(.*?)</td>", html)
# 打印<td>元素的内容
for element in td_elements:
print(element)
```
输出结果为:
```
1
2
```
阅读全文