pythonのスクレイピングについての質問です。
自分は、python初心者なので、お答え頂けると幸いです。
bitcoinの全ブロックのノンスの情報を取得したいのですが、エラーがはかれてしまいます。
最初のページからhtmlを取得した後、前のブロックのurlを参照して...を繰り返していきたいのです。
よろしくお願いします。
python
1import requests 2import csv 3from bs4 import BeautifulSoup 4import urllib.request as req 5 6res = req.urlopen('https://www.blockchain.com/ja/btc/block/000000000000000000049216eefe012a12db9666a934f2718b430e7c17e330dd') 7 8sum = 0 9 10with open('score.csv', 'w') as csv_file: 11 fieldnames = ['ブロック高', 'ノンス'] 12 writer = csv.DictWriter(csv_file, fieldnames = fieldnames) 13 writer.writeheader() 14 15 while True: 16 17 soup = BeautifulSoup(res.read(), "html.parser") 18 num = 0 19 nonce = 0 20 21 for info in soup.find_all('td'): 22 23 if num == 1: 24 nonce = info.string 25 num = 0 26 27 if num == 2: 28 res = requests.get('https://www.blockchain.com/ja/btc/block/' + info.string) 29 num = 0 30 31 if info.string == "ノンス": 32 num += 1 33 34 if info.string == "前のブロック": 35 num += 2 36 37 writer.writerow({'ブロック高': 573121 - sum, 'ノンス': nonce}) 38 print(573121 - sum, nonce) 39 40 sum += 1 41 42 if sum == 100: 43 break
AttributeError Traceback (most recent call last) <ipython-input-4-58203b933b20> in <module> 15 while True: 16 ---> 17 soup = BeautifulSoup(res.read(), "html.parser") 18 num = 0 19 nonce = 0 AttributeError: 'Response' object has no attribute 'read'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。