Python 无法从BeautifulSoup中提取文本

Python 无法从BeautifulSoup中提取文本,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用beautifulsoup和regex从其中一个html页面获取数据,但无法这样做 html_数据: <td class="col-a size a-update">200 MB<span class="next-size">1250</span></td> 200mb1250 我只想提取200 MB,但不想提取1250 MB 我尝试了以下代码: from bs4 import BeautifulSoup html_string

我正在尝试使用beautifulsoup和regex从其中一个html页面获取数据,但无法这样做

html_数据:

<td class="col-a size a-update">200 MB<span class="next-size">1250</span></td>
200mb1250
我只想提取200 MB,但不想提取1250 MB

我尝试了以下代码:

from bs4 import BeautifulSoup

html_string = '<td class="coll-4 size mob-uploader">194.5 MB<span 
class="seeds">3422</span></td>'
soup = BeautifulSoup(html_string, 'html.parser')
size =  soup.find('td', {'class': 'size'}).getText()
print size
from bs4 import BeautifulSoup

html_string = '<td class="coll-4 size mob-uploader">194.5 MB<span 
class="seeds">3422</span></td>'
soup = BeautifulSoup(html_string, 'html.parser')
size =  soup.find('td', {'class': 'size'}).contents[0]
print size
从bs4导入美化组
html_字符串='194.5 MB3422'
soup=BeautifulSoup(html_字符串'html.parser')
size=soup.find('td',{'class':'size'}).getText()
印刷尺寸
但我都得到了194.5 MB3422


请建议。

我已使用以下代码解决了此问题:

from bs4 import BeautifulSoup

html_string = '<td class="coll-4 size mob-uploader">194.5 MB<span 
class="seeds">3422</span></td>'
soup = BeautifulSoup(html_string, 'html.parser')
size =  soup.find('td', {'class': 'size'}).getText()
print size
from bs4 import BeautifulSoup

html_string = '<td class="coll-4 size mob-uploader">194.5 MB<span 
class="seeds">3422</span></td>'
soup = BeautifulSoup(html_string, 'html.parser')
size =  soup.find('td', {'class': 'size'}).contents[0]
print size
从bs4导入美化组
html_字符串='194.5 MB3422'
soup=BeautifulSoup(html_字符串'html.parser')
size=soup.find('td',{'class':'size'})。内容[0]
印刷尺寸