python - 用 Python bs4 从 HTML 中提取文本
问题描述
我正在尝试从中提取值, <div class="number">
如下图所示,但输出返回None
,我该如何获取该值?
的HTML:
我已经尝试过的代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from pylogix import PLC
my_url = 'https://www.aeso.ca/'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
report = page_soup.findAll("div",{"class":"number"})
print(report)
解决方案
该网站是动态加载的,因此requests
不支持它。我们可以使用Selenium作为抓取页面的替代方案。
安装它:pip install selenium
。
从这里下载正确的 ChromeDriver 。
from time import sleep
from selenium import webdriver
from bs4 import BeautifulSoup
URL = "https://www.aeso.ca/"
driver = webdriver.Chrome(r"c:\path\to\chromedriver.exe")
driver.get(URL)
# Wait for the page to fully render before parsing it
sleep(5)
# The source of the page is in the `page_source` method of the `driver`
soup = BeautifulSoup(driver.page_source, "html.parser")
driver.quit()
report = soup.find_all("div", {"class": "number"})
print(report)
输出:
[<div class="number">10421 <span class="unit">MW</span></div>, <div class="number">$37.57 <span class="unit">/ MWh</span></div>]
要仅获取文本,请调用该.text
方法:
for tag in report:
print(tag.text)
输出:
10421 MW
$37.57 / MWh
要仅获取“矿池价格”的输出,请使用 CSS 选择器:
print(soup.select_one(".chart-price div.number").text)
# Or uncomment this to only extract the price, and remove `/ MWh` from the output
# print(soup.select_one(".chart-price div.number").text.split("/")[0])
输出(当前):
$37.57 / MWh
推荐阅读
- audio - Youtube Red 停止下载视频的音频
- python - 简单的神经网络反向传播实现——梯度问题(scipy fmin_cg 优化器)
- java - 进行不同父字段初始化的最佳方法是什么?
- c - 如何在C中存储预先不知道大小的数字序列?
- javascript - 在 if 语句中添加多个 EventListener
- java - 覆盖 Spring bean
- javascript - 如何在javascript中迭代一个数组,在循环块中使用promise并等待所有promise完成以继续
- html - 居中导航栏 CSS
- c# - 仅使用 1 个 GET 时出现“AmbiguousActionException:匹配多个操作”
- mariadb - MariaDB - 外键约束不正确?