###前提・実現したいこと
スクレイピングした競馬データの1行目に、race_date~win_horseまでの見出しをつけること
###発生している問題・エラーメッセージ
1行目に見出しを読み込ませるスクリプトが分かりません。
###該当のソースコード
# -*- coding:utf-8 -*- import urllib2 import codecs import time from bs4 import BeautifulSoup f = codecs.open('horse.csv', 'w', 'utf-8') tpl_url='http://db.netkeiba.com/?pid=jockey_detail&id=00663&page={0}' for i in xrange(1, 2): url=tpl_url.format( i ) soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml") tr_arr = soup.select("table.race_table_01 > tbody > tr") for tr in tr_arr: tds = tr.findAll("td") time.sleep(1) race_date = tds[0].a.text #日付 kaisai = tds[1].a.text #開催 weather=tds[2].text #天気 race_number=tds[3].text #R race_name=tds[4].a.text #レース名 tousuu=tds[6].text #頭数 frame_number=tds[7].text #枠番 horse_number=tds[8].text #馬番 single_win_racio=tds[9].text #単勝 popularity=tds[10].text #人気 horse_arrival=tds[11].text #着順 horse_name=tds[12].a.text #馬名 weight=tds[13].text #斤量 distance=tds[14].text #距離 baba=tds[15].text #馬場 race_time=tds[16].text #タイム difference=tds[17].text #着差 horse_pass=tds[18].text #通過 pace=tds[19].text #ペース nobori=tds[20].text #上り horse_weight=tds[21].text #馬体重 win_horse=tds[22].a.text #勝ち馬 print race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\ single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,\ race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse cols = [race_date, kaisai,weather,race_number,race_name,tousuu,frame_number,\ horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,\ distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse] f.write(",".join(cols) + "\n") f.close()
###試したこと
このようなスクリプトでも上手く出来ませんでした
f.write(race_date+ ','+ kaisai+ ','+weather+ ','+race_number+ ','+race_name+ ','+tousuu+ ','+frame_number+ ','
+horse_number+ ','+single_win_racio+ ','+popularity+ ','+horse_arrival+ ','+horse_name+ ','+weight
+ ','+distance+ ','+baba+ ','+race_time+ ','+difference+ ','+horse_pass+ ','+pace+ ','+nobori+ ','+horse_weight+ ','
+win_horse+ ',' + u"\n")
###補足情報(言語/FW/ツール等のバージョンなど)
python2.7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/12 13:32
2016/08/12 13:53
2016/08/12 14:19