fork download
  1. from bs4 import BeautifulSoup
  2. import csv
  3. import os
  4. import re
  5. import requests
  6.  
  7. ## delete only if file exists ##
  8.  
  9. if os.path.exists('KOSPI200.csv'):
  10. os.remove('KOSPI200.csv')
  11. else:
  12. print("Sorry , I can not remove {} file.".format('KOPSPI200.csv'))
  13.  
  14. BaseUrl = 'https://f...content-available-to-author-only...r.com/sise/entryJongmok.nhn?&page='
  15.  
  16.  
  17. for i in range(1,22,1):
  18. try:
  19. url = BaseUrl + str(i)
  20. r = reuqests.get(url)
  21. soup = BeautifulSoup(r.text,'lxml')
  22. items = soup.find_all('td',{'class':'ctg'})
  23.  
  24. for item in items:
  25. #print(item)
  26. txt = item.a.get('href')
  27. k = re.search('[\d]+',txt)
  28. if k:
  29. code = k.group()
  30. name = item.text
  31. data = code, name
  32.  
  33. with open ('KOSPI200.csv','a') as f:
  34. writer=csv.writer(f)
  35. writer.writerow(data)
  36. except:
  37. pass
  38. finally:
  39. temp_for_sort = []
  40. with open ('KOSPI200.csv','r') as in_file:
  41. for sort_line in in_file:
  42. temp_for_sort.append(sort_line)
  43.  
  44. with open('KOSPI200.csv','w') as out_file:
  45. seen = set()
  46. for line in temp_for_sort:
  47. if line in seen: continue
  48.  
  49. seen.add(line)
  50. out_file.wrte(line)# your code goes here
Runtime error #stdin #stdout #stderr 0.05s 113856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 5, in <module>
ImportError: No module named requests