python-3.x - Beautifull Soup 刮网页所有赔率表
问题描述
我想使用以下代码从 URL 网页获取所有表格。
import csv
import requests
from bs4 import BeautifulSoup
urls = [
'https://g10oal.com/match/c81e21f3-7804-4961-ac74-4e2804a19784/odds'
]
all_data = []
for url in urls:
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
table = soup.findAll("class", {"class":"table table-sm odds-compare-table"})[0]
# here I store all rows to list `all_data`
for row in table.findAll('tr'):
tds = [cell.get_text(strip=True, separator=' ') for cell in row.findAll(["td", "th"])]
all_data.append(tds)
print(*tds)
# write list `all_data` to CSV
with open("c:/logs/test.csv", "wt+", newline="") as f:
writer = csv.writer(f)
for row in all_data:
writer.writerow(row)
运行代码后,显示“IndexError: list index out of range”
解决方案
第一个是argument
标签/元素的,而不是.findAll
name
attribute
你应该做
# make sure to use a single space only between table-sm and odds-compare-table
tables = soup.findAll("table", {"class": "table table-sm odds-compare-table"})
# or, pass the classes as list
tables = soup.findAll("table", {"class": ["table", "table-sm", "odds-compare-table"]})
然后你循环tables
for table in tables:
# here I store all rows to list `all_data`
for row in table.findAll('tr'):
tds = [cell.get_text(strip=True, separator=' ') for cell in row.findAll(["td", "th"])]
all_data.append(tds)
print(*tds)
如果你只想要第一个table
你可以使用find
而不是findAll
table = soup.find("table", {"class": ["table", "table-sm", "odds-compare-table"]})
推荐阅读
- ios - 无法在自定义视图类的 Storyboard 中看到 IBInspectable 自定义属性
- unix - 在 linux 中使用 hadoop fsck 命令时我们可以跳过文件检查吗?
- google-admin-sdk - 审计报告 API 限制增加
- c# - 我应该在哪个级别测试我的逻辑
- asp.net-core-2.0 - Kestrel 在哪里寻找要监听的端口?
- node.js - 带有 nginx 配置的 socket.io
- vue.js - Vue JS表格标签中如何正确使用v-if和v-else
- php - 我有多个与数组同名且值相同的单选按钮
- symfony - Symfony4 安全中的两种认证方式
- php - 在 wordpress 中找不到函数“remove_comment_support”或函数名无效