前提・実現したいこと
tabletag から全てのtrタグを取り出したい
発生している問題・エラーメッセージ
'NoneType' object has no attribute 'find_all'
該当のソースコード
Python3
1import requests 2from bs4 import BeautifulSoup 3 4url = "https://www.jalan.net/270000/LRG_272000/?stayYear=&stayMonth=&stayDay=&dateUndecided=1&stayCount=1&roomCount=1&adultNum=2&ypFlg=1&kenCd=270000&screenId=UWW1380&roomCrack=200000&lrgCd=272000&distCd=01&rootCd=04/page{}#" 5target_url = url.format(1) 6r = requests.get(target_url) 7soup = BeautifulSoup(r.content, "html.parser") 8 9d_list = [] 10products = soup.find_all("div", class_="p-yadoCassette__body p-searchResultItem__body") 11for product in products: 12 row = product.find("div", class_="p-yadoCassette__summary p-searchResultItem__summary") 13 table = product.find("table", class_="p-planTable p-searchResultItem__planTable") 14 15 name = row.find("h2", class_="p-searchResultItem__facilityName").text 16 17 tr_tags = table.find_all("tr")[1:3] 18 for tr_tag in tr_tags: 19 point_info,per,total = tr_tag.find_all("td") 20 21 point = point_info.find("li", class_="c-label c-label--orange p-searchResultItem__horizontalLabel overwritePointLabel") 22 perfee = per.find("span") 23 totalfee = total.find("span") 24 25 d = { 26 "name":name, 27 "point":point.text, 28 "perfee":perfee.text, 29 "totalfee":totalfee.text 30 } 31
試したこと
tableの中身などを確認してみたのですが、tdタグが複数あるのに対して取り出せない。
補足情報(FW/ツールのバージョンなど)
Python 3.9.0
find_allは、何ケ所もありますがどの行でエラーになったのか記入願います。
失礼しました。tr_tags = table.find_all("tr")[1:3]こちらの行になります。
「table = product.find("table", class_="p-planTable p-searchResultItem__planTable") 」で table がNoneなのですね。
find_allは見つからなかった場合、「[]」が返ってくるので、おかしいと思っていたのですが、元々がNoneなら納得です。ppaulさんの通りです。
蛇足ですが tableタグは、pandasのto_csvが便利です(tableだから)。
回答2件
あなたの回答
tips
プレビュー