python - BeautifulSoup 提取多个表
问题描述
我正在尝试使用 BeautifulSoup 从同一个 HTML 的两个表中提取一些数据。实际上,我已经从两个表中提取了一部分,但不是全部。这是我拥有的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html_content = urlopen('https://www.icewarehouse.com/Bauer_Vapor_X25_Ice_Hockey_Skates/descpage-V25XS.html')
soup = BeautifulSoup(html_content, "lxml")
tables = soup.find_all('table', attrs={'class' : 'orderingtable fl'})
for table_skates in tables:
t_headers = []
t_data = []
t_row = {}
for tr in table_skates.find_all('th'):
t_headers.append(tr.text.replace('\n', '').strip())
for td in table_skates.find_all('td'):
t_data.append(td.text.replace('\n', '').strip())
t_row = dict(zip(t_headers, t_data))
print(t_row)
这是我得到的输出:
{'Size': '1.0', 'Price': '$109.99', 'Stock': '1', 'Qty': ''}
{'Size': '7.0', 'Price': '$159.99', 'Stock': '2+', 'Qty': ''}
解决方案
推荐阅读
- xamarin - 如何在 Akavache 中订阅新价值?
- python - 从主机名中提取 AWS AZ
- c# - Dapper QueryAsync,返回一个列表
- html - 如何让这个简单的 css-grid 布局在 IE11 中工作
- c++ - 如何重载由日期和月份组成的对象的前置和后置增量运算符,以打印为 std::string?
- node.js - 在每个区域上创建具有特定权限的 MongoDB userSchema
- c# - 如何在 .NET Core 中自动发送 HttpClient get 方法
- android - 允许一个参数使用不同的类型,例如 String 和 StringRes int 的总和类型
- javascript - 如何从输入框中添加新的数组元素并将其显示在列表中?
- javascript - 替换url后如何运行