Advertisement
borsha06

Untitled

Feb 23rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Feb 23 19:31:53 2017
  4.  
  5. @author: borsha
  6. """
  7.  
  8. # -*- coding: utf-8 -*-
  9. """
  10. Created on Wed Feb 22 22:13:56 2017
  11.  
  12. @author: borsha
  13. """
  14.  
  15. import requests
  16. from bs4 import BeautifulSoup
  17. def get_page(url):
  18.     page=requests.get(url)
  19.     get_p= page.text
  20.     return get_p
  21.  
  22. def check(text):
  23.     return type(text)!=type(None) and len(text)>0 and len(text)<25 and text.find("\n")==-1  and text.find("/")==-1 and text.find("illion")==-1
  24.  
  25. def bs4_get(content,func):
  26.     soup=BeautifulSoup(content,"html.parser")
  27.     get_all=soup.find_all(string=func)
  28.     return get_all
  29.    
  30. def is_valid(text):
  31.    
  32.     for i in text:
  33.         if(not i.isalpha() and not i.isspace()):
  34.             return False
  35.     return True
  36.    
  37.  
  38. def code_get():
  39.  
  40.    
  41.     names=""
  42.     codes=""
  43.     count=0
  44.    
  45.     url="https://countrycode.org/"
  46.     t=get_page(url)
  47.     all_text=bs4_get(t,check)
  48.    
  49.     start=all_text.index("Afghanistan")
  50.     end=all_text.index("263",start) + 1
  51.     all_text=all_text[start:end]
  52.        
  53.    
  54.     file= open("code.txt","w")
  55.  
  56.    
  57.     for each in all_text:
  58.        
  59.         te = str(each)
  60.        
  61.         if(is_valid(te)):
  62.             count = 0
  63.        
  64.         if(count==0):
  65.             names = te
  66.         elif(count==1):
  67.             codes = te
  68.         elif(te=="-"):
  69.             x="1-234"
  70.             codes=x.replace("-","",5)
  71.         count+=1
  72.         if(count==2):
  73.             file.write("%-25s %-10s\n" % (names,codes))
  74.             names=""
  75.             codes=""
  76.  
  77.    
  78.     file.close()
  79.     return
  80.  
  81.  
  82. code_get()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement