python获取<span></span>中的数字
时间: 2024-05-04 16:14:05
浏览: 209
在Python中,你可以使用正则表达式或者BeautifulSoup库来获取HTML标签中的数字。以下是两种方法的示例:
方法一:使用正则表达式
```python
import re
html = '<span>123</span>'
pattern = r'<span>(\d+)</span>'
match = re.search(pattern, html)
if match:
number = match.group(1)
print(number)
方法二:使用BeautifulSoup库
```python
from bs4 import BeautifulSoup
html = '<span>123</span>'
soup = BeautifulSoup(html, 'html.parser')
span_tag = soup.find('span')
if span_tag:
number = span_tag.text
print(number)
```
相关问题
python 正在表达式获取<li>共 <span class="color">6,753</span> 条记录</li>中的6,753
同样可以使用正则表达式来提取这个字符串中的数字`6,753`。具体来说,可以先匹配`<li>`标签和`</li>`标签之间的内容,然后再从中提取数字部分。示例如下:
```python
import re
s = '<li>共 <span class="color">6,753</span> 条记录</li>'
match = re.search(r'<li>.*?<span.*?>([\d,]+)</span>.*?</li>', s)
if match:
```
<div class="priceBox"><p>本套房源的参考总价:<span>395万</span></p>python如何只获取数据‘395万’
相关推荐






网页源代码模板如下: <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/> </head> <body>[talkid:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是dffwerwer天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[ 音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[ 图片 [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已方式方法方式 [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我而对方是否的天吧! </body> </html> 利用python爬虫,提取源代码字符串,并从每行body中爬取[<a href=后的链接地址,如果没有则将该行链接地址设置为NONE








网页源代码模板如下: <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/> </head> <body>[talkid:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是dffwerwer天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[ 音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[ 图片 [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已方式方法方式 [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我而对方是否的天吧! </body> </html> 使用python爬虫提取body每行 中发言或 发送后的字段,包括a标签内的地址

网页源代码模板如下: <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/> </head> <body>[talkid:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是dffwerwer天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[ 音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[ 图片 [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已方式方法方式 [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我而对方是否的天吧! </body> </html> 使用python爬虫提取body每行 中发言或 发送后的字段,如果是“音频 :”或"图片 :"则输入[]内的文字
