我想从这个网站上得到表格,https://caniwin.com/poker/omahahilopreALL.php
我想写一个python脚本来获取这些数据,并将其放入csv中,这样我就可以按WinHi %
排序
我目前拥有的脚本就是这么做的
import requests
import csv
from bs4 import BeautifulSoup
# Fetch the HTML content from the website
url = 'https://caniwin.com/poker/omahahilopreALL.php'
response = requests.get(url)
html_content = response.text
# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')
# Find the table
table = soup.find('table')
print(table)
这样就可以很好地打印表格。问题是,由于我使用的是libre office,当我试图解析并将其放入逗号分隔的文件时,它看起来像janky或不起作用。
例如,这个脚本不会以我可以按我想要的值排序的方式输出它
import requests
import csv
from bs4 import BeautifulSoup
# Fetch the HTML content from the website
url = 'https://caniwin.com/poker/omahahilopreALL.php'
response = requests.get(url)
html_content = response.text
# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')
# Find the table
table = soup.find('table')
# Extract table data
table_data = []
for row in table.find_all('tr'):
row_data = []
for cell in row.find_all(['td']):
row_data.append(cell.text.strip())
table_data.append(row_data)
# Output table to CSV file
filename = 'output.csv'
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(table_data)
print(f"Table data has been saved to {filename}")
1条答案
按热度按时间y4ekin9u1#
您可以使用
pandas
创建数据框架并将其保存到CSV:图纸:
并保存
data.csv
(来自LibreOffice的屏幕截图):