[英]Get data from HTML page using python
I would like to get the value 100 from the tag below using python and beautiful soup 我想使用python和漂亮的汤从下面的标签中获取值100
<span style="font-size:90%"><b>100</b> <cite style="color:#cc0000"><b>-0.10</b> (0.52%)</cite></span>
The code below gives me the following output 下面的代码为我提供了以下输出
100 -0.10 (0.52%)
How can I extract only the value 100? 如何仅提取值100?
Code: 码:
from urllib.request import Request, urlopen
import bs4
import re
url = 'url.com'
req = Request(url, headers = {'User-Agent': 'Mozilla/5.0'})
page = urlopen(req).read()
soup = bs4.BeautifulSoup(page, 'html.parser')
data = soup.find('span',style=re.compile('font-size:90%'))
value = data.text
You can get the first element of soup.contents
: 您可以得到
soup.contents
的第一个元素:
from bs4 import BeautifulSoup as soup
d = soup(page, 'html.parser').find('span', {'style':'font-size:90%'}).contents[0].text
Output: 输出:
'100'
Just Find the <b>
tag it will give you 100. 只要找到
<b>
标记,它就会给您100。
data = soup.find('span',style=re.compile('font-size:90%'))
value = data.find('b').text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.