Last active
January 20, 2018 14:52
-
-
Save koheso/36285bba45bb17fea2101d226433ae4f to your computer and use it in GitHub Desktop.
スクレイピングでCoinmarketcapのデータをスクレイピング
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
import requests | |
from bs4 import BeautifulSoup | |
from datetime import datetime | |
#URLの指定 | |
url = 'https://coinmarketcap.com/' | |
#ユーザージェントの設定 | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", | |
} | |
r = requests.get(url, headers=headers) | |
soup = BeautifulSoup(r.text.encode(r.encoding),'lxml') | |
#trをすべて取得 | |
for tr in soup.find_all('tr'): | |
coinCon = tr.find("a") | |
if coinCon != None: | |
coinName = coinCon.text | |
price = tr.find("a", { "class" : "price" }) | |
rate = tr.find("td", {"class" : "percent-24h"}) | |
print("■" + coinName) | |
print("price:" + price.text) | |
print("rate24h:" + rate.text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment