python - 使用 BeautifulSoup1 从 html 表中获取数据
问题描述
content = soup.find_all("div", id = "ctl00_ContentPlaceHolder1_ctl03_divHO")
for book in content:
stock = book.find('I', {'class'= "Item_Price10"}).text
print (stock)
我想通过找出 CONTENT 中的值来使用 BS4 来获取股票价格,但代码不能正常工作。请帮助我,提前谢谢你
解决方案
该类Item_price10
似乎是<td>
标签的一部分,因此您可以尝试以下操作:
import requests
from bs4 import BeautifulSoup
req = requests.get("https://s.cafef.vn/Lich-su-giao-dich-FPT-1.chn")
soup = BeautifulSoup(req.content, "html.parser")
for div in soup.find_all("div", id = "ctl00_ContentPlaceHolder1_ctl03_divHO"):
for tr in div.find_all('tr'):
for td in tr.find_all('td', {'class': "Item_Price10"}):
print(td.text)
print()
这将显示以下内容:
95.70
95.70
3,325,700
319,480,000,000
1,243,600
95.50
97.00
95.40
0
0
0
94.70
94.70
...
推荐阅读
- r - 使用 purrr、broom 和 dplyr1.0.0 中的几个不同预测器将 glm 应用于相同的结果变量
- oracle - 推荐的表设计 - APEX 21.1 / Oracle 19c
- python - 添加最后一列以计算百分比变化
- jenkins - 如何将詹金斯节点列表带入活动选择参数并仅显示当前登录用户的节点
- c - switch-case 执行时间是否可变?
- python - 如何找到周期 t=1...50的 k t+1的值?
- python - OperationalError:查询期间丢失与 MySQL 服务器的连接
- python - 将 python 文件导入另一个(在子目录中)时,它会停止在同一目录中找到 csv
- python - 使用 unittest.mock 的补丁装饰器和 new 获取补丁对象
- python - 如何使用 Python 在微分方程组中找到最大化目标的变量值?(批量反应器优化)