怎么把里面的文字提取出来
时间: 2023-10-26 21:04:51 浏览: 49
要提取出文字,您可以通过以下两种方法之一进行操作:
1. 使用HTML解析库:您可以使用Python中的BeautifulSoup或其他类似的库来解析HTML代码,然后提取出所需的文字内容。您可以使用这些库的选择器或XPath来定位和提取特定元素中的文本。
示例代码:
```python
from bs4 import BeautifulSoup
html = '<div style="color: rgb(34, 34, 34); line-height: normal;"><span style="font-size: 16px;">一、(本题20分)</span></div>'
soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()
print(text)
```
输出结果:
```
一、(本题20分)
```
2. 使用正则表达式:如果您对正则表达式较为熟悉,也可以使用正则表达式来提取出所需的文本内容。
示例代码:
```python
import re
html = '<div style="color: rgb(34, 34, 34); line-height: normal;"><span style="font-size: 16px;">一、(本题20分)</span></div>'
text = re.sub('<[^<]+?>', '', html)
print(text)
```
输出结果:
```
一、(本题20分)
```
这些方法可以帮助您从HTML代码中提取出所需的文字内容。请根据您的具体需求选择适合您的方法。