而我使用bs4来解析si

2024-05-14 10:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用bs4解析Bitmex中的价格信息。你知道吗

(网站url是https://www.bitmex.com/app/trade/XBTUSD

所以,我写下了这样的代码

from bs4 import BeautifulSoup
import requests

url = 'https://www.bitmex.com/app/trade/XBTUSD'
bitmex = requests.get(url)

if bitmex.status_code == 200:
    print("connected...")
else:
    print("Error...")

bitmex_html = bitmex.text
soup = BeautifulSoup(bitmex_html , 'lxml' )
price = soup.find_all("span", {"class": "price"})
print(price)

结果是这样的

connected...
[]

为什么会弹出“[]”?而要把价格文本像'6065.5',我该怎么办? 我要分析的文本是

<span class="price">6065.5</span>

选择器是

内容>;分区>;分区tickerBar.overflown>;分割>;span.instruments.tickerBarSection节&燃气轮机;第n个孩子(1) >;跨度价格你知道吗

我只是研究Python,所以这个问题对pro来说似乎很奇怪…抱歉


Tags: httpsimportgtcomappurlwww价格
2条回答

您的问题是页面首先不包含那些span元素。如果您在浏览器开发工具中选中response选项卡(在firefox中按F12),您可以看到页面由script标记组成,其中一些代码是用javascript编写的,在执行时动态创建元素。你知道吗

因为BeautifulSoup不能执行Javascript,所以不能直接用它提取元素。你有两种选择:

  • 使用类似于selenium的东西,它允许您从python驱动浏览器—这意味着javascript将被执行,因为您使用的是真正的浏览器—但是性能会受到影响。你知道吗
  • 阅读javascript代码,理解它并编写python代码来模拟它。这通常是困难的,但幸运的是,这似乎非常简单的页面,你想要:

    import requests
    import lxml.html
    
    r = requests.get('https://www.bitmex.com/app/trade/XBTUSD')
    doc = lxml.html.fromstring(r.text)
    data = json.loads(doc.xpath("//script[@id='initialData']/text()")[0])
    

正如您所看到的,数据在页面中是json格式的。加载数据变量后,可以使用它访问所需的信息:

for row in data['orderBook']:
    print(row['symbol'], row['price'], row['side'])

将打印:

('XBTUSD', 6051.5, 'Sell')
('XBTUSD', 6051, 'Sell')
('XBTUSD', 6050.5, 'Sell')
('XBTUSD', 6050, 'Sell')

你很接近。尝试一下下面的方法,看看是否更符合你的要求。可能您看到或检索的格式与您所期望的不完全相同。希望这有帮助。你知道吗

from bs4 import BeautifulSoup
import requests
import sys
import json

url = 'https://www.bitmex.com/app/trade/XBTUSD'
bitmex = requests.get(url)

if bitmex.status_code == 200:
    print("connected...")
else:
    print("Error...")
    sys.exit(1)

bitmex_html = bitmex.text
soup = BeautifulSoup(bitmex_html , 'lxml' )

# extract the json text from the returned page
price = soup.find_all("script", {"id": "initialData"})
price = price.pop()

# parse json text
d = json.loads(price.text)

# pull out the order book and then each price listed in the order book
order_book = d['orderBook']
prices = [v['price'] for v in order_book]
print(prices)

输出示例:

connected...
[6045, 6044.5, 6044, 6043.5, 6043, 6042.5, 6042, 6041.5, 6041, 6040.5, 6040, 6039.5, 6039, 6038.5, 6038, 6037.5, 6037, 6036.5, 6036, 6035.5, 6035, 6034.5, 6034, 6033.5, 6033, 6032.5, 6032, 6031.5, 6031, 6030.5, 6030, 6029.5, 6029, 6028.5, 6028, 6027.5, 6027, 6026.5, 6026, 6025.5, 6025, 6024.5, 6024, 6023.5, 6023, 6022.5, 6022, 6021.5, 6021, 6020.5]

相关问题 更多 >

    热门问题