python - 用漂亮的汤刮网页 4. 删除基于类的 tr 元素。Python
问题描述
我正在从这个页面抓取:' https://kenpom.com/index.php?y=2018 '
我有以下代码:
import requests
from bs4 import BeautifulSoup
url ='https://kenpom.com/index.php?y=2018'
r = requests.get(url).text
soup = BeautifulSoup(r, 'lxml')
table = soup.find('table',{'id':'ratings-table'}).tbody
teams = table.findAll('tr')4
该变量teams
包含 367 个tr
元素。它们中的大多数没有类,但其中一些具有“thead1”类,有些具有“thead2”类。如何删除具有“thead1”或“thead2”作为类的所有tr
元素?teams
解决方案
尝试这个:
[x for x in teams if 'class' not in x.attrs or ('thead1' not in x.attrs['class'] and 'thead2' not in x.attrs['class'])]
推荐阅读
- c# - 如何将 App.xaml 构建操作从应用程序定义更改为页面
- rust - 如何在 HashMap 中找到值的键?
- google-apps-script - 如何使用 Google Apps 脚本向表单添加“文件上传问题”?
- docker - Docker-compose 产生错误:详细信息:用户“”的密码不匹配。db_1 | 连接匹配 pg_hba.conf 第 95 行:“host all all all md5”
- excel - 调用其他工作表的子时出现错误“424”
- python - 将 sympy Symbol 转换为字符串以便始终可以解析它?
- python - 将json文件合并为一个的最佳方法
- elasticsearch - 来自 filebeat 的 Logstash 输出。什么是“索引”配置选项?
- visual-studio-code - Elm VSCode 插件在保存时不格式化
- hyperledger-fabric - 超级账本——合约实例化问题