Advertisement
borsha06

Untitled

Feb 22nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Feb 22 22:13:56 2017
  4.  
  5. @author: borsha
  6. """
  7.  
  8. # -*- coding: utf-8 -*-
  9. """
  10. Created on Wed Feb 22 15:19:48 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.  
  31. def code_get(info,full_info):
  32.  
  33.    names=""
  34.    codes=""
  35.    count=0;
  36.    
  37.    
  38.    
  39.    url="https://countrycode.org/"
  40.    t=get_page(url)
  41.    all_text=bs4_get(t,check)
  42.    
  43.    start=all_text.index("Afghanistan")
  44.    end=all_text.index("263",start)
  45.    all_text=all_text[start:end+1]
  46.    
  47.    
  48.    file= open("cod.txt",'w')
  49.    #print (all_text)
  50.    
  51.  
  52.    for each in all_text:
  53.         te = str(each)
  54.         print (te)
  55.        
  56.         if(count==0):
  57.             names=te
  58.         elif(count==1):
  59.             codes=te
  60.         count+=1
  61.         if(count==4):
  62.             info=(names,codes)
  63.             full_info.append(info)
  64.            
  65.             count=0
  66.             names=""
  67.             codes=""
  68.            
  69.        # if(te=="Afghanistan"):
  70.             #found=True
  71.             for i in range(len(full_info)):
  72.                 file.write("%20s %20s" % (full_info[i][0],full_info[i][1]))
  73.                 file.write("\n")
  74.    file.close()
  75.    return
  76. name=[]
  77. c=[]
  78.  
  79. full_info=[]
  80. info=[]
  81. code_get(info,full_info)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement