mmyjh86

[러닝-인강] Globa-Rates #5

Aug 18th, 2020 (edited)
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import re
  3. from bs4 import BeautifulSoup
  4.      
  5. def scraping():
  6.     p = re.compile(r"\xa0")
  7.    
  8.     url = "https://www.global-rates.com/en/interest-rates/libor/american-dollar/usd-libor-interest-rate-3-months.aspx"
  9.     resp = requests.get(    url   )
  10.     soup = BeautifulSoup(resp.text, 'html5lib')
  11.  
  12.     # 금리 스크래핑
  13.     selector = "form table tbody tr td table tbody tr td table tbody tr:nth-child(1) td:nth-child(1) table tbody tr td:nth-child(2)"
  14.     result = soup.select(   selector )
  15.     value = [ p.sub('', x.text.strip()[:-1]) for x in result[1:] ]
  16.            
  17.     # 날짜 스크래핑
  18.     selector = "form table tbody tr td table tbody tr td table tbody tr:nth-child(1) td:nth-child(1) table tbody tr td:nth-child(1)"
  19.     result = soup.select(   selector )
  20.     date = [ p.sub('', x.text.replace(' ', '-')) for x in result[4:14] ]
  21.    
  22.     # 딕셔너리로 저장
  23.     data = {     }
  24.     for i in range(len(date)):
  25.         data[ date[i] ] = value[i]
  26.     return data
  27.  
  28. def read_database(filename):
  29.     try:
  30.         f = open(filename, "r")
  31.         lines = f.readlines()
  32.        
  33.         # 리스트를 딕셔너리로 변환
  34.         data = {  }
  35.         for line in lines:
  36.             k, v = line.split(' ')
  37.             data[k] = v.replace('\n', '')
  38.         return data
  39.  
  40.     except FileNotFoundError:
  41.         return [ ]
  42.  
  43. def write_database(filename, data):
  44.     f = open(filename, "w")
  45.     for k, v in data.items():
  46.         f.write(f"{k} {v}" + "\n")
  47.  
  48. # ------------------------------------------------------------------
  49.  
  50. data = read_database("result.txt")
  51.  
  52. newcommer = scraping()
  53. data.update(newcommer)
  54.  
  55. write_database("result.txt", data)
RAW Paste Data