속도 비교
requests json > requests html > selenium
import requests
import pandas as pd # 데이터 처리를 위한
page, page_size = 1, 60
url = f'https://m.stock.naver.com/api/index/KOSPI/price?\pageSize={page_size}&page={page}'
response = requests.get(url)
response
다음과 같이 200을 출력한다면 정상적으로 응답을 받아온 것이다.
columns = ["localTradedAt", "closePrice"]
data = response.json()
kospi_df = pd.DataFrame(data)[columns]
kospi_df
import requests, json
import pandas as pd # 데이터 처리를 위한
REST_API_KEY = "your-key"
resultType = 'json'
basDt = '20241105'
url = f'http://apis.data.go.kr/1160100/service/GetStockSecuritiesInfoService/getStockPriceInfo?serviceKey={REST_API_KEY}&resultType={resultType}&basDt={basDt}'
response = requests.get(url)
response
data = repsonse.json()['response']['body']['items']['item']
df = pd.DataFrame(data)
df
import pandas as pd
import requests
from bs4 import BeautifulSoup
query = "삼성전자"
url = f"https://search.naver.com/search.naver?query={query}"
dom = BeautifulSoup(response.text, "html.parser")
elements = dom.select(".fds-refine-query-grid a")
elements[0].text
element = elements[0]
link = element.get("href")