2024-04-23 10:56:36 发布
网友
我正在为学校做一个项目,我显示比特币的当前价格,以太币,也许还有另外一种,还有即时消息网络抓取https://cryptowat.ch/,但是我找不到用来存储实时价格的标签。当我解析div标签时,它会返回价格,但我无法将其分离出来,以便用python显示它
<div class="rankings-col__header__segment"><h2>BTC</h2><weak>usd </weak>10857.00</div>
据我所知-您知道BTC字符串,可以使用它来创建定位器。你知道吗
BTC
因此,如果是XPath,可以使用它和following-sibling::text():
following-sibling::text()
//h2[. = 'BTC']/following-sibling::text()
使用lxml.html的示例:
lxml.html
from lxml.html import fromstring data = """<div class="rankings-col__header__segment"><h2>BTC</h2><weak>usd </weak>10857.00</div>""" root = fromstring(data) print(root.xpath("//h2[. = 'BTC']/following-sibling::text()"))
打印['10857.00']。你知道吗
['10857.00']
如果您碰巧使用BeautifulSoup,那么它将是:
BeautifulSoup
from bs4 import BeautifulSoup data = """<div class="rankings-col__header__segment"><h2>BTC</h2><weak>usd </weak>10857.00</div>""" soup = BeautifulSoup(data, "html.parser") print(soup.find("h2", string="BTC").find_next_sibling(text=True))
据我所知-您知道
BTC
字符串,可以使用它来创建定位器。你知道吗因此,如果是XPath,可以使用它和
following-sibling::text()
:使用
lxml.html
的示例:打印
['10857.00']
。你知道吗如果您碰巧使用
BeautifulSoup
,那么它将是:相关问题 更多 >
编程相关推荐