2016-09-02 241 views
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文件的上榜出問題了嗎?

+0

使用兒童[2]你選擇DOM中的第3個表。這是否正是你想要的,DOM中的數據fron第三表? –

回答

-1

你有my_table所以使用findfind_allmy_table得到<tr>後來<td>,然後你可以從<td>得到text


編輯:

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') 
    if len(cols) >= 10: 
     print(cols[0].text.strip(),'|',end='') 
     print(cols[2].text.strip(),'|',end='') 
     print(cols[4].text.strip(),'|',end='') 
     print(cols[7].text.strip(),'|',end='') 
     print(cols[8].text.strip(),'|',end='') 
     print(cols[9].text.strip(),'|',end='') 
     print() 
     print('-'*40) 

結果

Match No. |Teams(Home vs Away) |Expected StopSelling Time |Home/Away/Draw | | | 
---------------------------------------- 
FRI 9 |Romania U21 vs Luxembourg U21 |03/09 01:30 |Accept In Play Betting Only | | | 
---------------------------------------- 
FRI 13 |St. Vincent and Grenadines vs USA |03/09 03:30 |35.00 |13.00 |1.02 | 
---------------------------------------- 
FRI 14 |Honduras vs Canada |03/09 05:06 |1.45 |3.55 |6.50 | 
---------------------------------------- 
FRI 15 |Trinidad and Tobago vs Guatemala |03/09 07:00 |1.67 |3.20 |4.70 | 
----------------------------------------