###前提・実現したいこと
python初心者でございます。
競馬の騎手のデータを集めております。
beautifulsoupでスクレイピングしたCSVレースデータ1列目に騎手の名前を挿入したいのですが
うまく出来ません。
###発生している問題・エラーメッセージ
C:\Users\satoru\horse>kami-2.py Traceback (most recent call last): File "C:\Users\satoru\horse\kami-2.py", line 46, in <module> print jockey_name,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 UnicodeEncodeError: 'cp932' codec can't encode character u'\xa0' in position 4: illegal multibyte se quence
###該当のソースコード
# -*- coding:utf-8 -*- import urllib2 import codecs import time from bs4 import BeautifulSoup f = codecs.open('horse.csv','w','utf-8') f.write('jockey_name,race_date,kaisai,weather,race_number,race_name,tousuu,frame_number,horse_number,single_win_ratio,popularity,horse_arrival,horse_name,weight,distance,baba, race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse'+ u"\n") 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") jockey_name = soup.find('div',{'class':'db_head_name'}).find('h1').text.replace("\n","") 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 jockey_name,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 = [jockey_name,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()
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
回答1件
あなたの回答
tips
プレビュー