获取html中style=text_indent的内容
时间: 2024-05-16 21:15:32 浏览: 114
可以使用Python的BeautifulSoup库来解析HTML并获取指定的内容。具体步骤如下:
1. 安装BeautifulSoup库:
```
pip install beautifulsoup4
```
2. 使用BeautifulSoup解析HTML文档:
```python
from bs4 import BeautifulSoup
html_doc = '<html><head><title>Test</title></head><body><p style="text-indent: 2em;">This is a paragraph.</p></body></html>'
soup = BeautifulSoup(html_doc, 'html.parser')
```
3. 查找指定的内容:
```python
p_tag = soup.find('p', {'style': 'text-indent: 2em;'})
if p_tag:
text_indent = p_tag.get('style').split(':')[1].strip()
print(text_indent)
else:
print('No text-indent found.')
```
解释一下上面的代码:
- 首先使用`find`方法查找第一个`<p>`标签,并且`style`属性值为`text-indent: 2em;`。
- 如果找到了指定的标签,则获取它的`style`属性值,并根据冒号分割后获取第二个部分,也就是`2em`。
- 如果没找到指定的标签,则输出提示信息。
最终输出结果为:
```
2em
```
阅读全文
相关推荐
















