方法:使用python与xpath,提取span.price里的数据
场景:可以定时运行,每隔五分钟在屏幕上打印一次btc的实时价位,把握好时机被噶韭菜。
xpath:在chrome里的f12里的console输入$x("//a[@href='/zh-cn/exchange/btc_usdt/']/span[@class='price']/text()")[0],可以得到btc的实时数据
代码如下:
import requests from lxml import etree url = 'https://www.huobi.me/zh-cn/exchange/btc_usdt/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', } page_text = requests.get(url=url, headers=headers).text tree = etree.HTML(page_text) pricebtc = tree.xpath("//a[@href='/zh-cn/exchange/btc_usdt/']/span[@class='price']/text()") print(pricebtc) ~
但是运行后的结果是[],不知道是什么原因?有没有大神能帮忙解读一下?