【发布时间】:2024-04-26 17:40:01
【问题描述】:
我正在尝试使用 Python2.7 和 BeautifulSoup4 从我的电力供应商的website 获取当前的“5 分钟趋势价格”。
xpath 是:xpath = "//html/body/div[2]/div/div/div[3]/p[1]"
或
<div class="instant prices">
<p class="price">
"5.2" # this is what I'm ultimately after
<small>¢</small>
<strong> per kWh </strong>
</p>
我尝试了无数不同的方法来获取 “5.2” 值,并成功地深入到“即时价格”对象,但无法从中获取任何信息.
我当前的代码如下所示: 导入 urllib2 从 bs4 导入 BeautifulSoup
url = "https://rrtp.comed.com/live-prices/"
soup = BeautifulSoup(urllib2.urlopen(url).read())
#print soup
instantPrices = soup.findAll('div', 'instant prices')
print instantPrices
...输出为:
[<div class="instant prices">
</div>]
[]
无论如何,“即时价格”对象似乎是空的,尽管我在 Chrome 中检查元素时可以清楚地看到它。 任何帮助将不胜感激!谢谢!
【问题讨论】:
标签: html python-2.7 web-scraping beautifulsoup