주식을 기준으로 네이버에서 PER을 뽑아보자
import requests
from bs4 import BeautifulSoup
url = "https://finance.naver.com/item/main.naver?code=005930"
html = requests.get(url).text
soup = BeautifulSoup(html, "html5lib")
tags = soup.select("#_per")
tag = tags[0]
print(tag.text)
코드를 조금 더 정리해보자
import requests
from bs4 import BeautifulSoup
def get_per(code):
url = "https://finance.naver.com/item/main.naver?code=" + code
html = requests.get(url).text
soup = BeautifulSoup(html, "html5lib")
tags = soup.select("#_per")
tag = tags[0]
return tag.text
def get_dividend(code):
url = "https://finance.naver.com/item/main.naver?code=" + code
html = requests.get(url).text
soup = BeautifulSoup(html, "html5lib")
tags = soup.select("#_dvr")
tag = tags[0]
return tag.text
print(get_per("005930"))
print(get_dividend("005930"))
ID가 없는경우라면??
테이블(tbody) 내의 tr 내의 td 내의 em 값을 출력하려 한다.
import requests
from bs4 import BeautifulSoup
url = "http://finance.naver.com/item/main.nhn?code=005930"
html = requests.get(url).text
soup = BeautifulSoup(html, "html5lib")
# tags = soup.select("table tbody tr td em")
tags = soup.select("#tab_con1 > div:nth-of-type(2) > table > tbody > tr.strong > td > em")
for tag in tags:
print(tag.text)
RestfulAPI를 이용해 비트코인 데이터 받기
import requests
r = requests.get("https://api.korbit.co.kr/v1/ticker/detailed?currency_pair=btc_krw")
bitcoin = r.json()
print(bitcoin)
print(type(bitcoin))
print(bitcoin['last'])
print(bitcoin['bid'])
print(bitcoin['ask'])
print(bitcoin['volume'])