borsha06

Untitled

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