LoginSignup
1
0

More than 5 years have passed since last update.

PythonでNPBのチームデータが取りたい。(球団ごとのPDFファイルをDL)

Last updated at Posted at 2018-05-10

選手会のHPからソースを参照してaタグの部分を抜き出し
hrefのところから該当のpathを特定
ファイル名をチーム名.pdfでDLする。

getPDF_DL.py
import bs4
import requests
import urllib3

url = requests.get('http://jpbpa.net/register/')
url.raise_for_status()
# HTMLparserでHTMLのaタグだけ絞り込み
soup = bs4.BeautifulSoup(url.text, "html.parser")
elems = soup.select('a')
for elem in elems:
    team = elem.getText()
    path = elem.get('href')
    repath = path.replace('..', 'http://jpbpa.net')
    # elem.getText=球団名、elem.get('href')=PDFのpath
    if elem.getText() in {'ロッテ', 'ソフトバンク', '西武', '楽天', 'オリックス', '日本ハム',
                          '広島', '阪神', 'DeNA', '巨人', '中日', 'ヤクルト'}:
        #print('{}({})'.format(team, repath))
        # pdfのDLチーム名.pdfにする。
        request_methods = urllib3.PoolManager()
        response = request_methods.request('GET', repath)
        f = open(team + '.pdf', 'wb')
        f.write(response.data)
        f.close()
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0