###前提・実現したいこと
python初心者でございます。
競馬情報をスクレイピングしておりますが、下記のことが、うまくできません。
勝ち馬の情報を最後に取ってこようとしましたが、うまくできないこと。
###発生している問題・エラーメッセージ
File "C:\Users\satoru\horse\horse-test-kami-1.py", line 39, in <module> win_horse=tds[26].a.text #蜍昴■鬥ャ・茨シ堤捩鬥ャ・・ AttributeError: 'NoneType' object has no attribute 'text'
###該当のソースコード
# -*- coding:utf-8 -*- import urllib2 import codecs import time from bs4 import BeautifulSoup f = codecs.open('horse.csv', 'w', 'utf-8') f.write('horse_name,race_date,kaisai,weather,race_number,race_name,tousuu,frame_number,horse_number,horse_odds,popularity,horse_arrival,jockey_name,weight,distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse'+u"\n") url='http://db.netkeiba.com/horse/1994103997/' soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml") tr_arr = soup.select("table.db_h_race_results > tbody > tr") for tr in tr_arr: tds = tr.findAll("td") 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 #馬番 horse_odds=tds[9].text #オッズ popularity=tds[10].text #人気 horse_arrival=tds[11].text #着順 jockey_name=tds[12].a.text #騎手名 weight=tds[13].text #斤量 distance=tds[14].text #距離 baba=tds[15].text #馬場 race_time=tds[17].text #タイム difference=tds[18].text #着差 horse_pass=tds[20].text #通過 pace=tds[21].text #ペース nobori=tds[22].text #上り horse_weight=tds[23].text #馬体重 win_horse=tds[26].a.text #勝ち馬(2着馬):ここの箇所がエラーになります horse_name_tag = soup.find('div',{'class':'horse_title'}).find('h1') horse_name = "".join([x for x in horse_name_tag.text if not x == u'\xa0' and not x == u'\n']) print horse_name.strip(),race_date.strip(),kaisai.strip(),weather.strip(),race_name.strip(),tousuu.strip(),frame_number.strip(),horse_number.strip(),horse_odds.strip(),popularity.strip(),horse_arrival.strip(),jockey_name.strip(),weight.strip(),distance.strip(),baba.strip(),race_time.strip(),difference.strip(),horse_pass.strip(),pace.strip(),nobori.strip(),horse_weight.strip(),win_horse.strip() cols = [horse_name,race_date,kaisai,weather,race_number,race_name,tousuu,frame_number,horse_number,horse_odds,popularity,horse_arrival,jockey_name,weight,distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse] f.write(",".join(cols) + "\n") f.close()
###試したこと
textをstringに変えたりしましたが、うまく出来ませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
python2.7

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/16 12:06