###前提・実現したいこと
python初心者でございます。
python2.7,beautifulsoupで競馬情報のスクレイピングをしておりますが、途中から、うまく出来ません。
###発生している問題・エラーメッセージ
weather=tds[2].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') 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].a.text #天気 race_number=td[3].a.text #第○レース race_name=td[4].a.text #レース名 tousuu=td[5].a.text #頭数 frame_number=td[6].a.text #枠番 horse_number=td[7].a.text #馬番 single_win_racio=td[8].a.text #単勝 popularity=td[9].a.text #人気 horse_arrival=td[10].a.text #着順 horse_name=td[11].a.text #馬名 weight=td[12].a.text #斤量 distance=td[13].a.text #距離 baba=td[14].a.text #馬場 race_time=td[15].a.text #タイム difference=td[16].a.text #着差 horse_pass=tds[17].a.text #通過 pace=td[18].a.text #ペース nobori=td[19].a.text #上り horse_weight=td[20].a.text #馬体重 win_horse=td[21].a.text #勝ち馬 prize_money=td[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,prize_money 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,prize_money] f.write(",".join(cols) + "\n") f.close()
###試したこと
race_date、kaisaiまでは上手く動くのですが、天気以降のプログラムが動きません。
このサイトの先輩に御教示頂きました、tdsの意味も調べたのですが分からず困っております。
tdから探すのに、なぜtdsというスクリプトなのかも理解できない初心者でございます。
#開催以降のスクリプトが全く分からない状態でございます。
よろしく御教示お願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
python2.7
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。