몸풀기
1.환율정보
import requests
from bs4 import BeautifulSoup
url = "<https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%ED%99%98%EC%9C%A8%EC%A0%95%EB%B3%B4>"
res = requests.get(url)
# print(res.text)
soup = BeautifulSoup(res.content, 'html.parser')
환율 = soup.find("span", attrs={"class":"spt_con dw"}).find("strong").get_text()
print(환율)
2.날씨정보
3.국가별 환율정보
import requests
from bs4 import BeautifulSoup
url = "<https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=%ED%99%98%EC%9C%A8%EC%A0%95%EB%B3%B4&oquery=%EC%84%9C%EC%9A%B8%EB%82%A0%EC%94%A8&tqi=hly6Msp0JywssQmFnOhssssst0w-148262>"
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
# ["미국, USD, 1197, 0.40, -0.03", "일본", "중국"]
rows = soup.find("table", attrs={"class":"rate_table_info"}).find("tbody").find_all("tr")
for row in rows:
print("="*10)
# print(row)
country_name = row.find("th").find("span").get_text()
print(country_name)
country_rate = row.find("td").find("span").get_text()
print(country_rate)
sign = ""
country_rate2 = row.find("td", attrs={"class":"flu_nm"}).find("em").get_text()
arrow = row.find("td", attrs={"class":"flu_nm"}).find("span", attrs={"class":"ico"}).get_text()
print(arrow)
if arrow =="상승":
sign = "+"
else:
sign = "-"
print(sign + country_rate2)
3-1.국가별 환율데이터 (2) - 쉬운버전
import requests
from bs4 import BeautifulSoup
url = "<https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=%ED%99%98%EC%9C%A8%EC%A0%95%EB%B3%B4&oquery=%EC%84%9C%EC%9A%B8%EB%82%A0%EC%94%A8&tqi=hly6Msp0JywssQmFnOhssssst0w-148262>"
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
table = soup.find("table", attrs={"class":"rate_table_info"}).find("tbody").find_all("tr")
# print(table)
index = 0
for row in table:
index = index + 1
print("=" * 30)
print(str(index) + "번째입니다.")
print(row)
국가이름 = row.find("th").find("span").get_text()
print(국가이름)
매매기준율 = row.find("td").find("span").get_text()
매매기준율 = 매매기준율.replace(",", "")
print(매매기준율)
전일대비 = row.find("td", attrs={"class":"flu_nm"}).find("em").get_text()
print(전일대비)
등락률 = row.find("td", attrs={"class":"flu_pct"}).find("span").get_text()
print(등락률)
file = open("test.csv", "a", encoding='UTF-8-sig')
file.write("{}, {}, {}, {} \\n".format(국가이름, 매매기준율, 전일대비, 등락률))
file.close()