Created
August 23, 2017 06:12
-
-
Save monchy-monchy/3f4f1cdc5934ea7773916757b6aa299f to your computer and use it in GitHub Desktop.
jpx_new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python3 | |
# jpx_new.py | |
import requests, os, bs4 | |
# 対象ページのURLの設定 | |
url = 'http://www.jpx.co.jp/listing/stocks/new/' | |
# ダウンロードしたPDFファイルを保管するためのフォルダを作成 | |
os.makedirs('jpx_new', exist_ok=True) | |
# webページをダウンロードしてbs4で煮込む。 | |
# ステータスをプリント | |
print('downloading web page {}...'.format(url)) | |
res = requests.get(url) | |
res.raise_for_status() | |
soup = bs4.BeautifulSoup(res.text, "lxml") | |
# PDFファイルを抽出するためのタグの選択。a要素のhrefの中から、"1s.pdf"を含むものを抽出。 | |
pdf_1s = soup.select('a[href$="1s.pdf"]') | |
# 何も見つからなかったら「見つかりませんでした」とプリントして終了 | |
if pdf_1s == []: | |
print('見つかりませんでした') | |
else: | |
# 見つかったら、pdf_1sの要素を順に抽出していく | |
for i in range(0,len(pdf_1s)): | |
pdf_1s_url = 'http://www.jpx.co.jp' + pdf_1s[i].get('href') | |
print('downloading pdf file {}...'.format(pdf_1s_url)) | |
res = requests.get(pdf_1s_url) | |
res.raise_for_status() | |
# PDFファイルの保存 | |
pdf_file = open(os.path.join('jpx_new', os.path.basename(pdf_1s_url)), 'wb') | |
for chunk in res.iter_content(100000): | |
pdf_file.write(chunk) | |
pdf_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment