Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Wed Feb 22 22:13:56 2017
- @author: borsha
- """
- # -*- coding: utf-8 -*-
- """
- Created on Wed Feb 22 15:19:48 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("\t")==-1 and text.find("/")==-1 and text.find("illion")==-1 and text.find(",")==-1
- def bs4_get(content,func):
- soup=BeautifulSoup(content,"html.parser")
- get_all=soup.find_all(string=func)
- return get_all
- def code_get(info,full_info):
- 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)
- all_text=all_text[start:end+1]
- file= open("cod.txt",'w')
- #print (all_text)
- for each in all_text:
- te = str(all_text)
- if(count==0):
- names=te
- elif(count==1):
- codes=te
- count+=1
- if(count==2):
- info=(names,codes)
- full_info.append(info)
- count=0
- names=""
- codes=""
- # if(te=="Afghanistan"):
- #found=True
- for i in range(len(full_info)):
- file.write("%20s %20s" % (full_info[i][0],full_info[i][1]))
- file.write("\n")
- file.close()
- return
- name=[]
- c=[]
- full_info=[]
- info=[]
- code_get(info,full_info)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement