market = soup.find('td', class_='cellcolor_1')
クラス指定が列:人気を取得するコードになっていますが・・・
◇スクレイピングの基本。
取得したい要素の上位のタグから下に手繰っていく。
HTMLのセレクタ指定と発想は同じです。
Python
1# -*- coding: utf-8 -*-
2from urllib.request import urlopen
3from bs4 import BeautifulSoup
4
5
6def main() -> None:
7 # 指定するURL
8 URL = "http://race.netkeiba.com/?pid=payback_list&id=p2018050301"
9 html = urlopen(URL)
10 soup = BeautifulSoup(html, "html.parser")
11 print(soup)
12
13 for table in soup.find_all('table', class_='race_table_01'):
14 for val in table.find_all('td', class_='txt_r'):
15 print(val.text)
16 break
17
18
19if __name__ == '__main__':
20 main()
21
◇参考情報
[netkeiba] の検索結果
218/06/10追記
Python
1# -*- coding: utf-8 -*-
2from itertools import filterfalse
3from urllib.request import urlopen
4from bs4 import BeautifulSoup
5
6
7def main() -> None:
8 # 指定するURL
9 URL = "http://race.netkeiba.com/?pid=payback_list&id=p2018050301"
10 html = urlopen(URL)
11 soup = BeautifulSoup(html, "html.parser")
12 print(soup)
13 print('#' * 60)
14
15 for tr in soup.select("table.race_table_01 > tr.bml1"):
16 for rank in filterfalse(lambda x: x.renderContents().decode() != "1", tr.select("td:nth-of-type(1)")):
17 for popular in tr.select("td:nth-of-type(12)"):
18 print(popular.renderContents().decode())
19 break
20 # filterfalseは以下の行と同じです
21 #for rank in tr.select("td:nth-of-type(1)"):
22 # if rank.renderContents().decode() != "1":
23 # continue
24 # for popular in tr.select("td:nth-of-type(12)"):
25 # print(popular.renderContents().decode())
26 # break
27
28
29if __name__ == '__main__':
30 main()
31
◇参考情報
0. itertools.filterfalse
0. Pythonスクレイピング:同じ名前のクラス内での識別
0. :nth-of-type()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/10 12:49
2018/06/10 12:56 編集
2018/06/10 13:31
2018/06/11 15:23
2018/06/11 17:37 編集
2018/06/12 03:34