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文件的上榜出问题了吗?
2016-09-02
Y Y
+0
使用儿童[2]你选择DOM中的第3个表。这是否正是你想要的,DOM中的数据fron第三表? –