0
import requests
from bs4 import BeautifulSoup
url = "http://bet.hkjc.com/football/index.aspx?lang=en"
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")
div = soup.find("div", {"class": "footballmaincontent"})
tables = div.find_all("table")
my_table = tables[2]
for row in my_table.find_all('tr'):
cols = row.find_all('td')
odds_list = []
if len(cols) >= 10:
match_no = (cols[0].text.strip())
teams = (cols[2].text.strip())
match_time = (cols[4].text.strip())
home_odds = (cols[7].text.strip())
away_odds = (cols[8].text.strip())
draw_odds = (cols[9].text.strip())
odds_row = (match_no,teams,match_time,home_odds,away_odds,draw_odds)
odds_list.append(odds_row)
# Write to csv file
import csv
with open("odds_file.csv", "wb") as file:
writer = csv.writer(file)
for row in odds_list:
writer.writerow(row)
我試圖通過將列附加到for循環中的「odds_list」來將列導出到csv文件。但事實證明,它沒有在「odds_file」中寫入任何內容。用bs4抓取網頁
我知道那裏有與
odds_row = (match_no,teams,match_time,home_odds,away_odds,draw_odds)
但我怎麼能追加,我到CSV文件的上榜出問題了嗎?
使用兒童[2]你選擇DOM中的第3個表。這是否正是你想要的,DOM中的數據fron第三表? –