Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Thu Feb 23 19:31:53 2017
- @author: borsha
- """
- # -*- coding: utf-8 -*-
- """
- Created on Wed Feb 22 22:13:56 2017
- @author: borsha
- """
- import requests
- from bs4 import BeautifulSoup
- def get_page(url):
- page=requests.get(url)
- get_p= page.text
- return get_p
- def check(text):
- 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
- def bs4_get(content,func):
- soup=BeautifulSoup(content,"html.parser")
- get_all=soup.find_all(string=func)
- return get_all
- def is_valid(text):
- for i in text:
- if(not i.isalpha() and not i.isspace()):
- return False
- return True
- def code_get():
- names=""
- codes=""
- count=0
- url="https://countrycode.org/"
- t=get_page(url)
- all_text=bs4_get(t,check)
- start=all_text.index("Afghanistan")
- end=all_text.index("263",start) + 1
- all_text=all_text[start:end]
- file= open("code.txt","w")
- for each in all_text:
- te = str(each)
- if(is_valid(te)):
- count = 0
- if(count==0):
- names = te
- elif(count==1):
- codes = te
- elif(te=="-"):
- x="1-234"
- codes=x.replace("-","",5)
- count+=1
- if(count==2):
- file.write("%-25s %-10s\n" % (names,codes))
- names=""
- codes=""
- file.close()
- return
- code_get()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement