In [1]:
#https://en.wikipedia.org/wiki
import requests
from bs4 import BeautifulSoup
import re
import json
import pickle
import datetime

In [2]:
def name_formatter(name):
    name = re.sub("_", " ", name).strip().title()
    return(name)

In [6]:
name_formatter("Mohammad_Najibullah")


Out[6]:
'Mohammad Najibullah'

In [3]:
def ar_lookup(name):
    """
    Convert names from English to Arabic using European Media Monitors and 
     code modified from Phil Schrodt.
    """
    name = name_formatter(name)
    base_url = "http://emm.newsexplorer.eu/NewsExplorer/search/en/entities?query="
    url = base_url + name
    
    try:
        page = requests.get(url)
        soup = BeautifulSoup(page.content, "lxml")
        name_url = soup.find("p", {"class" : "center_headline"}).find("a")['href']
    except Exception as e:
        #print("Couldn't get page of results back: ", e)
        return []

    try:
        base = "http://emm.newsexplorer.eu/NewsExplorer/search/en/"
        name_url = base + name_url
        name_page = requests.get(name_url)
        soup = BeautifulSoup(name_page.content, "lxml")
        # check to make sure in list???? Take in alt names???/petrarch2/petrarch2/data/dictionaries
        names = soup.find("td", {"colspan" : "1"}).find_all("p")
        names = [i.text for i in names][1:]
        names_en = [i for i in names if re.search("\(.*?Eu|\(.*?en", i)]
        names_en = [re.sub("\s+?\(.+?\)", "", name) for name in names_en]
        #print("Found match. Matched English name: ", names_en[0])
        names_ar = [i for i in names if re.search("\(.*?ar", i)]
        names_ar = [re.sub("\s+?\(.+?\)", "", name) for name in names_ar]
        return names_ar
    except Exception:
        traceback.print_exc()
        return []

In [140]:
import traceback

In [157]:
#this will need the user to pass in an english name and come back with an arabic name
def hack_wiki(eng_name):
    base_url="https://en.wikipedia.org/wiki/"+eng_name
    try:
        page=requests.get(base_url)
        soup=BeautifulSoup(page.content,"lxml")
        name=soup.find(id="firstHeading").contents
        print(name)
        ar_url=soup.find("li",{"class":"interwiki-ar"}).find("a")['href']
        print(ar_url)
        ar_page=requests.get(ar_url)
        ar_soup=BeautifulSoup(ar_page.content,"lxml")
        ar_name=ar_soup.find(id="firstHeading").contents
        return {eng_name:ar_name}
        #print("name "+name+" url: "+str(ar_url))
    except Exception as e:
        #traceback.print_stack()
        print(e)

In [192]:
base_url="https://en.wikipedia.org/wiki/"+"MOHAMMAD_ZAHIR_SHAH"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
    'Content-Type': 'text/html',
}
page=requests.get(base_url,headers=headers)
soup=BeautifulSoup(page.content,"lxml")
name=soup.find(id="firstHeading").contents
print(name)
ar_url=soup.find("li",{"class":"interwiki-ar"}).find("a")['href']
print(ar_url)


['MOHAMMAD ZAHIR SHAH']
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-192-b5378ec8175e> in <module>()
      8 name=soup.find(id="firstHeading").contents
      9 print(name)
---> 10 ar_url=soup.find("li",{"class":"interwiki-ar"}).find("a")['href']
     11 print(ar_url)

AttributeError: 'NoneType' object has no attribute 'find'

In [189]:
soup


Out[189]:
<!DOCTYPE html>
<html class="client-nojs" dir="ltr" lang="en">
<head>
<meta charset="utf-8"/>
<title>MOHAMMAD ZAHIR SHAH - Wikipedia</title>
<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>
<script>(window.RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"MOHAMMAD_ZAHIR_SHAH","wgTitle":"MOHAMMAD ZAHIR SHAH","wgCurRevisionId":0,"wgRevisionId":0,"wgArticleId":0,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"MOHAMMAD_ZAHIR_SHAH","wgRelevantArticleId":0,"wgRequestId":"WVUbiApAMFcAAHWQbkgAAABJ","wgIsProbablyEditable":false,"wgRelevantPageIsProbablyEditable":false,"wgRestrictionCreate":[],"wgFlaggedRevsParams":{"tags":{}},"wgStableRevisionId":null,"wgWikiEditorEnabledModules":{"toolbar":true,"preview":false,"publish":false},"wgBetaFeaturesFeatures":[],"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":false,"wgPopupsShouldSendModuleToUser":false,"wgPopupsConflictsWithNavPopupGadget":false,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","usePageImages":true,"usePageDescriptions":true},"wgPreferredVariant":"en","wgMFExpandAllSectionsUserOption":false,"wgMFDisplayWikibaseDescriptions":{"search":true,"nearby":true,"watchlist":true,"tagline":false},"wgRelatedArticles":null,"wgRelatedArticlesUseCirrusSearch":true,"wgRelatedArticlesOnlyUseCirrusSearch":false,"wgULSCurrentAutonym":"English","wgNoticeProject":"wikipedia","wgCentralNoticeCookiesToDelete":[],"wgCentralNoticeCategoriesUsingLegacy":["Fundraising","fundraising"],"wgCategoryTreePageCategoryOptions":"{\"mode\":0,\"hideprefix\":20,\"showcount\":true,\"namespaces\":false}","wgCentralAuthMobileDomain":false,"wgVisualEditorToolbarScrollOffset":0,"wgVisualEditorUnsupportedEditParams":["preload","preloadparams","preloadtitle","undo","undoafter","veswitched"],"wgEditSubmitButtonLabelPublish":false});mw.loader.state({"ext.globalCssJs.user.styles":"ready","ext.globalCssJs.site.styles":"ready","site.styles":"ready","noscript":"ready","user.styles":"ready","user":"ready","user.options":"loading","user.tokens":"loading","ext.visualEditor.desktopArticleTarget.noscript":"ready","ext.uls.interlanguage":"ready","ext.wikimediaBadges":"ready","mediawiki.legacy.shared":"ready","mediawiki.legacy.commonPrint":"ready","mediawiki.sectionAnchor":"ready","mediawiki.skinning.interface":"ready","skins.vector.styles":"ready","ext.globalCssJs.user":"ready","ext.globalCssJs.site":"ready"});mw.loader.implement("user.options@0gii3s4",function($,jQuery,require,module){mw.user.options.set([]);});mw.loader.implement("user.tokens@1dqfd7l",function ( $, jQuery, require, module ) {
mw.user.tokens.set({"editToken":"+\\","patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"});/*@nomin*/;

});mw.loader.load(["site","mediawiki.page.startup","mediawiki.user","mediawiki.hidpi","mediawiki.page.ready","mediawiki.searchSuggest","ext.gadget.teahouse","ext.gadget.ReferenceTooltips","ext.gadget.watchlist-notice","ext.gadget.DRN-wizard","ext.gadget.charinsert","ext.gadget.refToolbar","ext.gadget.extra-toolbar-buttons","ext.gadget.switcher","ext.gadget.featured-articles-links","ext.centralauth.centralautologin","mmv.head","mmv.bootstrap.autostart","ext.visualEditor.desktopArticleTarget.init","ext.visualEditor.targetLoader","ext.eventLogging.subscriber","ext.wikimediaEvents","ext.navigationTiming","ext.uls.eventlogger","ext.uls.init","ext.uls.interface","ext.centralNotice.geoIP","ext.centralNotice.startUp","skins.vector.js"]);});</script>
<link href="/w/load.php?debug=false&amp;lang=en&amp;modules=ext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediaBadges%7Cmediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.sectionAnchor%7Cmediawiki.skinning.interface%7Cskins.vector.styles&amp;only=styles&amp;skin=vector" rel="stylesheet"/>
<script async="" src="/w/load.php?debug=false&amp;lang=en&amp;modules=startup&amp;only=scripts&amp;skin=vector"></script>
<meta content="" name="ResourceLoaderDynamicStyles"/>
<link href="/w/load.php?debug=false&amp;lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=vector" rel="stylesheet"/>
<meta content="MediaWiki 1.30.0-wmf.6" name="generator"/>
<meta content="origin-when-cross-origin" name="referrer"/>
<meta content="noindex,nofollow" name="robots"/>
<link href="android-app://org.wikipedia/http/en.m.wikipedia.org/wiki/MOHAMMAD_ZAHIR_SHAH" rel="alternate"/>
<link href="/static/apple-touch/wikipedia.png" rel="apple-touch-icon"/>
<link href="/static/favicon/wikipedia.ico" rel="shortcut icon"/>
<link href="/w/opensearch_desc.php" rel="search" title="Wikipedia (en)" type="application/opensearchdescription+xml"/>
<link href="//en.wikipedia.org/w/api.php?action=rsd" rel="EditURI" type="application/rsd+xml"/>
<link href="//creativecommons.org/licenses/by-sa/3.0/" rel="copyright"/>
<link href="https://en.wikipedia.org/wiki/MOHAMMAD_ZAHIR_SHAH" rel="canonical"/>
<link href="//login.wikimedia.org" rel="dns-prefetch"/>
<link href="//meta.wikimedia.org" rel="dns-prefetch"/>
</head>
<body class="mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-MOHAMMAD_ZAHIR_SHAH rootpage-MOHAMMAD_ZAHIR_SHAH skin-vector action-view"> <div class="noprint" id="mw-page-base"></div>
<div class="noprint" id="mw-head-base"></div>
<div class="mw-body" id="content" role="main">
<a id="top"></a>
<div class="mw-body-content" id="siteNotice"><!-- CentralNotice --></div>
<div class="mw-indicators mw-body-content">
</div>
<h1 class="firstHeading" id="firstHeading" lang="en">MOHAMMAD ZAHIR SHAH</h1>
<div class="mw-body-content" id="bodyContent">
<div id="siteSub">From Wikipedia, the free encyclopedia</div>
<div id="contentSub"></div>
<div class="mw-jump" id="jump-to-nav">
					Jump to:					<a href="#mw-head">navigation</a>, 					<a href="#p-search">search</a>
</div>
<div class="mw-content-ltr" dir="ltr" id="mw-content-text" lang="en"><div class="noarticletext mw-content-ltr" dir="ltr" lang="en">
<table class="plainlinks fmbox fmbox-system" id="noarticletext" role="presentation"><tr><td class="mbox-text" style="padding: 0.6em 0.9em;"><div class="infobox" id="sisterproject" style="width: 20em; font-size: 90%; float: right; padding: 0.5em;">Look for <b>MOHAMMAD ZAHIR SHAH</b> on one of Wikipedia's <a href="/wiki/Special:SiteMatrix" title="Special:SiteMatrix">sister projects</a>:
<table cellpadding="1" cellspacing="0" style="background: none; margin: auto;">
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wiktionary-logo-v2.svg"><img alt="Wiktionary-logo-v2.svg" data-file-height="391" data-file-width="391" height="30" src="//upload.wikimedia.org/wikipedia/en/thumb/0/06/Wiktionary-logo-v2.svg/30px-Wiktionary-logo-v2.svg.png" srcset="//upload.wikimedia.org/wikipedia/en/thumb/0/06/Wiktionary-logo-v2.svg/45px-Wiktionary-logo-v2.svg.png 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/0/06/Wiktionary-logo-v2.svg/60px-Wiktionary-logo-v2.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wiktionary.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wiktionary:Special:Search/MOHAMMAD ZAHIR SHAH">Wiktionary</a> (free dictionary)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikibooks-logo.svg"><img alt="Wikibooks-logo.svg" data-file-height="300" data-file-width="300" height="30" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikibooks-logo.svg/30px-Wikibooks-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikibooks-logo.svg/45px-Wikibooks-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikibooks-logo.svg/60px-Wikibooks-logo.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikibooks.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikibooks:Special:Search/MOHAMMAD ZAHIR SHAH">Wikibooks</a> (free textbooks)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikiquote-logo.svg"><img alt="Wikiquote-logo.svg" data-file-height="355" data-file-width="300" height="30" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikiquote-logo.svg/25px-Wikiquote-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikiquote-logo.svg/38px-Wikiquote-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wikiquote-logo.svg/51px-Wikiquote-logo.svg.png 2x" width="25"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikiquote.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikiquote:Special:Search/MOHAMMAD ZAHIR SHAH">Wikiquote</a> (quotations)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikisource-logo.svg"><img alt="Wikisource-logo.svg" data-file-height="430" data-file-width="410" height="30" src="//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/29px-Wikisource-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/43px-Wikisource-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/57px-Wikisource-logo.svg.png 2x" width="29"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikisource.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikisource:Special:Search/MOHAMMAD ZAHIR SHAH">Wikisource</a> (free library)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikiversity-logo.svg"><img alt="Wikiversity-logo.svg" data-file-height="800" data-file-width="1000" height="24" src="//upload.wikimedia.org/wikipedia/commons/thumb/9/91/Wikiversity-logo.svg/30px-Wikiversity-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/9/91/Wikiversity-logo.svg/45px-Wikiversity-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/9/91/Wikiversity-logo.svg/60px-Wikiversity-logo.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikiversity.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikiversity:Special:Search/MOHAMMAD ZAHIR SHAH">Wikiversity</a> (free learning resources)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Commons-logo.svg"><img alt="Commons-logo.svg" data-file-height="1376" data-file-width="1024" height="30" src="//upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/22px-Commons-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/33px-Commons-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/45px-Commons-logo.svg.png 2x" width="22"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://commons.wikimedia.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="commons:Special:Search/MOHAMMAD ZAHIR SHAH">Commons</a> (images and media)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikivoyage-Logo-v3-icon.svg"><img alt="Wikivoyage-Logo-v3-icon.svg" data-file-height="193" data-file-width="193" height="30" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Wikivoyage-Logo-v3-icon.svg/30px-Wikivoyage-Logo-v3-icon.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Wikivoyage-Logo-v3-icon.svg/45px-Wikivoyage-Logo-v3-icon.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Wikivoyage-Logo-v3-icon.svg/60px-Wikivoyage-Logo-v3-icon.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikivoyage.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikivoyage:Special:Search/MOHAMMAD ZAHIR SHAH">Wikivoyage</a> (free travel guide)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikinews-logo.svg"><img alt="Wikinews-logo.svg" data-file-height="415" data-file-width="759" height="16" src="//upload.wikimedia.org/wikipedia/commons/thumb/2/24/Wikinews-logo.svg/30px-Wikinews-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/2/24/Wikinews-logo.svg/45px-Wikinews-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/2/24/Wikinews-logo.svg/60px-Wikinews-logo.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://en.wikinews.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikinews:Special:Search/MOHAMMAD ZAHIR SHAH">Wikinews</a> (free news source)</td>
</tr>
<tr style="height: 30px;">
<td style="text-align: center; vertical-align: middle;"><a class="image" href="/wiki/File:Wikidata-logo.svg"><img alt="Wikidata-logo.svg" data-file-height="590" data-file-width="1050" height="17" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/30px-Wikidata-logo.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/45px-Wikidata-logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/60px-Wikidata-logo.svg.png 2x" width="30"/></a></td>
<td style="vertical-align: middle;"><a class="extiw" href="https://www.wikidata.org/wiki/Special:Search/MOHAMMAD_ZAHIR_SHAH" title="wikidata:Special:Search/MOHAMMAD ZAHIR SHAH">Wikidata</a> (free linked database)</td>
</tr>
</table></div><b>Wikipedia does not have an article with this exact name.</b> Please <span class="plainlinks"><a class="external text" href="//en.wikipedia.org/w/index.php?search=MOHAMMAD+ZAHIR+SHAH&amp;title=Special%3ASearch&amp;fulltext=1">search for <i>MOHAMMAD ZAHIR SHAH</i> in Wikipedia</a></span> to check for alternative titles or spellings.
<ul><li> <a class="external text" href="//en.wikipedia.org/w/index.php?title=Special:UserLogin&amp;returnto=MOHAMMAD+ZAHIR+SHAH">Log in or create an account</a> to start the <i><b>MOHAMMAD ZAHIR SHAH</b></i> article, alternatively use the <a href="/wiki/Wikipedia:Article_wizard" title="Wikipedia:Article wizard">Article Wizard</a>, or <a href="/wiki/Wikipedia:Requested_articles" title="Wikipedia:Requested articles">add a request for it</a>.</li>
<li> <span class="plainlinks"><a class="external text" href="//en.wikipedia.org/w/index.php?search=MOHAMMAD+ZAHIR+SHAH&amp;title=Special%3ASearch&amp;fulltext=1">Search for "<i>MOHAMMAD ZAHIR SHAH</i>"</a></span> in existing articles.</li>
<li> <a href="/wiki/Special:WhatLinksHere/MOHAMMAD_ZAHIR_SHAH" title="Special:WhatLinksHere/MOHAMMAD ZAHIR SHAH">Look for pages within Wikipedia that link to this title</a>.</li></ul>
<div id="noarticletext_technical">
<hr/>
<p><b>Other reasons this message may be displayed:</b>
</p>
<ul><li> If a page was recently created here, it may not be visible yet because of a delay in updating the database; wait a few minutes or <a class="external text" href="//en.wikipedia.org/w/index.php?title=MOHAMMAD_ZAHIR_SHAH&amp;action=purge">try the purge function</a>.</li>
<li> Titles on Wikipedia are <b><a href="/wiki/Case_sensitivity" title="Case sensitivity">case sensitive</a></b> except for the first character; please <span class="plainlinks"><a class="external text" href="//en.wikipedia.org/w/index.php?search=MOHAMMAD+ZAHIR+SHAH&amp;title=Special%3ASearch&amp;fulltext=1">check alternative capitalizations</a></span> and consider adding a <a href="/wiki/Wikipedia:Redirect" title="Wikipedia:Redirect">redirect</a> here to the correct title.</li>
<li> If the page has been deleted, <a class="external text" href="//en.wikipedia.org/w/index.php?title=Special:Log/delete&amp;page=MOHAMMAD_ZAHIR_SHAH">check the <b>deletion log</b></a>, and see <a href="/wiki/Wikipedia:Why_was_the_page_I_created_deleted%3F" title="Wikipedia:Why was the page I created deleted?">Why was the page I created deleted?</a>.</li></ul>
</div></td></tr></table>
</div>
<noscript><img alt="" height="1" src="//en.wikipedia.org/wiki/Special:CentralAutoLogin/start?type=1x1" style="border: none; position: absolute;" title="" width="1"/></noscript></div> <div class="printfooter">
						Retrieved from "<a dir="ltr" href="https://en.wikipedia.org/wiki/MOHAMMAD_ZAHIR_SHAH">https://en.wikipedia.org/wiki/MOHAMMAD_ZAHIR_SHAH</a>"					</div>
<div class="catlinks catlinks-allhidden" data-mw="interface" id="catlinks"></div> <div class="visualClear"></div>
</div>
</div>
<div id="mw-navigation">
<h2>Navigation menu</h2>
<div id="mw-head">
<div aria-labelledby="p-personal-label" class="" id="p-personal" role="navigation">
<h3 id="p-personal-label">Personal tools</h3>
<ul>
<li id="pt-anonuserpage">Not logged in</li><li id="pt-anontalk"><a accesskey="n" href="/wiki/Special:MyTalk" title="Discussion about edits from this IP address [n]">Talk</a></li><li id="pt-anoncontribs"><a accesskey="y" href="/wiki/Special:MyContributions" title="A list of edits made from this IP address [y]">Contributions</a></li><li id="pt-createaccount"><a href="/w/index.php?title=Special:CreateAccount&amp;returnto=MOHAMMAD+ZAHIR+SHAH" title="You are encouraged to create an account and log in; however, it is not mandatory">Create account</a></li><li id="pt-login"><a accesskey="o" href="/w/index.php?title=Special:UserLogin&amp;returnto=MOHAMMAD+ZAHIR+SHAH" title="You're encouraged to log in; however, it's not mandatory. [o]">Log in</a></li> </ul>
</div>
<div id="left-navigation">
<div aria-labelledby="p-namespaces-label" class="vectorTabs" id="p-namespaces" role="navigation">
<h3 id="p-namespaces-label">Namespaces</h3>
<ul>
<li class="selected new" id="ca-nstab-main"><span><a accesskey="c" href="/w/index.php?title=MOHAMMAD_ZAHIR_SHAH&amp;action=edit&amp;redlink=1" title="View the content page [c]">Article</a></span></li>
<li class="new" id="ca-talk"><span><a accesskey="t" href="/w/index.php?title=Talk:MOHAMMAD_ZAHIR_SHAH&amp;action=edit&amp;redlink=1" rel="discussion" title="Discussion about the content page [t]">Talk</a></span></li>
</ul>
</div>
<div aria-labelledby="p-variants-label" class="vectorMenu emptyPortlet" id="p-variants" role="navigation">
<h3 id="p-variants-label">
<span>Variants</span><a href="#"></a>
</h3>
<div class="menu">
<ul>
</ul>
</div>
</div>
</div>
<div id="right-navigation">
<div aria-labelledby="p-views-label" class="vectorTabs emptyPortlet" id="p-views" role="navigation">
<h3 id="p-views-label">Views</h3>
<ul>
</ul>
</div>
<div aria-labelledby="p-cactions-label" class="vectorMenu emptyPortlet" id="p-cactions" role="navigation">
<h3 id="p-cactions-label"><span>More</span><a href="#"></a></h3>
<div class="menu">
<ul>
</ul>
</div>
</div>
<div id="p-search" role="search">
<h3>
<label for="searchInput">Search</label>
</h3>
<form action="/w/index.php" id="searchform">
<div id="simpleSearch">
<input accesskey="f" id="searchInput" name="search" placeholder="Search Wikipedia" title="Search Wikipedia [f]" type="search"/><input name="title" type="hidden" value="Special:Search"/><input class="searchButton mw-fallbackSearchButton" id="mw-searchButton" name="fulltext" title="Search Wikipedia for this text" type="submit" value="Search"/><input class="searchButton" id="searchButton" name="go" title="Go to a page with this exact name if it exists" type="submit" value="Go"/> </div>
</form>
</div>
</div>
</div>
<div id="mw-panel">
<div id="p-logo" role="banner"><a class="mw-wiki-logo" href="/wiki/Main_Page" title="Visit the main page"></a></div>
<div aria-labelledby="p-navigation-label" class="portal" id="p-navigation" role="navigation">
<h3 id="p-navigation-label">Navigation</h3>
<div class="body">
<ul>
<li id="n-mainpage-description"><a accesskey="z" href="/wiki/Main_Page" title="Visit the main page [z]">Main page</a></li><li id="n-contents"><a href="/wiki/Portal:Contents" title="Guides to browsing Wikipedia">Contents</a></li><li id="n-featuredcontent"><a href="/wiki/Portal:Featured_content" title="Featured content – the best of Wikipedia">Featured content</a></li><li id="n-currentevents"><a href="/wiki/Portal:Current_events" title="Find background information on current events">Current events</a></li><li id="n-randompage"><a accesskey="x" href="/wiki/Special:Random" title="Load a random article [x]">Random article</a></li><li id="n-sitesupport"><a href="https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&amp;utm_medium=sidebar&amp;utm_campaign=C13_en.wikipedia.org&amp;uselang=en" title="Support us">Donate to Wikipedia</a></li><li id="n-shoplink"><a href="//shop.wikimedia.org" title="Visit the Wikipedia store">Wikipedia store</a></li> </ul>
</div>
</div>
<div aria-labelledby="p-interaction-label" class="portal" id="p-interaction" role="navigation">
<h3 id="p-interaction-label">Interaction</h3>
<div class="body">
<ul>
<li id="n-help"><a href="/wiki/Help:Contents" title="Guidance on how to use and edit Wikipedia">Help</a></li><li id="n-aboutsite"><a href="/wiki/Wikipedia:About" title="Find out about Wikipedia">About Wikipedia</a></li><li id="n-portal"><a href="/wiki/Wikipedia:Community_portal" title="About the project, what you can do, where to find things">Community portal</a></li><li id="n-recentchanges"><a accesskey="r" href="/wiki/Special:RecentChanges" title="A list of recent changes in the wiki [r]">Recent changes</a></li><li id="n-contactpage"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us" title="How to contact Wikipedia">Contact page</a></li> </ul>
</div>
</div>
<div aria-labelledby="p-tb-label" class="portal" id="p-tb" role="navigation">
<h3 id="p-tb-label">Tools</h3>
<div class="body">
<ul>
<li id="t-whatlinkshere"><a accesskey="j" href="/wiki/Special:WhatLinksHere/MOHAMMAD_ZAHIR_SHAH" title="List of all English Wikipedia pages containing links to this page [j]">What links here</a></li><li id="t-upload"><a accesskey="u" href="/wiki/Wikipedia:File_Upload_Wizard" title="Upload files [u]">Upload file</a></li><li id="t-specialpages"><a accesskey="q" href="/wiki/Special:SpecialPages" title="A list of all special pages [q]">Special pages</a></li><li id="t-info"><a href="/w/index.php?title=MOHAMMAD_ZAHIR_SHAH&amp;action=info" title="More information about this page">Page information</a></li> </ul>
</div>
</div>
<div aria-labelledby="p-lang-label" class="portal" id="p-lang" role="navigation">
<h3 id="p-lang-label">Languages</h3>
<div class="body">
<ul>
</ul>
</div>
</div>
</div>
</div>
<div id="footer" role="contentinfo">
<ul id="footer-places">
<li id="footer-places-privacy"><a class="extiw" href="https://wikimediafoundation.org/wiki/Privacy_policy" title="wmf:Privacy policy">Privacy policy</a></li>
<li id="footer-places-about"><a href="/wiki/Wikipedia:About" title="Wikipedia:About">About Wikipedia</a></li>
<li id="footer-places-disclaimer"><a href="/wiki/Wikipedia:General_disclaimer" title="Wikipedia:General disclaimer">Disclaimers</a></li>
<li id="footer-places-contact"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact Wikipedia</a></li>
<li id="footer-places-developers"><a href="https://www.mediawiki.org/wiki/Special:MyLanguage/How_to_contribute">Developers</a></li>
<li id="footer-places-cookiestatement"><a href="https://wikimediafoundation.org/wiki/Cookie_statement">Cookie statement</a></li>
<li id="footer-places-mobileview"><a class="noprint stopMobileRedirectToggle" href="//en.m.wikipedia.org/w/index.php?title=MOHAMMAD_ZAHIR_SHAH&amp;mobileaction=toggle_view_mobile">Mobile view</a></li>
</ul>
<ul class="noprint" id="footer-icons">
<li id="footer-copyrightico">
<a href="https://wikimediafoundation.org/"><img alt="Wikimedia Foundation" height="31" src="/static/images/wikimedia-button.png" srcset="/static/images/wikimedia-button-1.5x.png 1.5x, /static/images/wikimedia-button-2x.png 2x" width="88"/></a> </li>
<li id="footer-poweredbyico">
<a href="//www.mediawiki.org/"><img alt="Powered by MediaWiki" height="31" src="/static/images/poweredby_mediawiki_88x31.png" srcset="/static/images/poweredby_mediawiki_132x47.png 1.5x, /static/images/poweredby_mediawiki_176x62.png 2x" width="88"/></a> </li>
</ul>
<div style="clear:both"></div>
</div>
<script>(window.RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgBackendResponseTime":121,"wgHostname":"mw1252"});});</script>
</body>
</html>

In [175]:
hack_wiki("MOHAMMAD_ZAHIR_SHAH")


['MOHAMMAD ZAHIR SHAH']
'NoneType' object has no attribute 'find'

In [118]:
base_url="https://en.wikipedia.org/wiki/"+"Mohammad_Najibullah"
page=requests.get(base_url)
soup=BeautifulSoup(page.content,"lxml")
lists=soup.select(".infobox tr th a")

In [119]:
for item in lists:
    print(item.contents)


['President of Afghanistan']
["General Secretary of the Central Committee of the People's Democratic Party of Afghanistan"]
['Director of the State Intelligence Agency']
['Alma mater']

In [117]:
base_url="https://en.wikipedia.org/wiki/"+"Mohammad_Najibullah"
page=requests.get(base_url)
soup=BeautifulSoup(page.content,"lxml")
#lists=soup.select(".infobox tr th a")
lists=soup.find("table",{"class":"infobox"}).find_all("tr")
for item in lists:
     if(item.find("td") is not None):
        if(item.find("td").find("span",{"class":"nowrap"}) is not None):
            if(item.find("td").find("span",{"class":"nowrap"}).parent is not None):
            #if(item.find("td").find("span",{"class":"nowrap"}).find("td") is not None):
                tempstr=item.find("td").find("span",{"class":"nowrap"}).parent.contents
                strstr=str(tempstr)
                if("display:none" not in strstr):
                    print(strstr.split('\\n')[1][:-2].replace("\\xa0",''))
                
#$('.infobox tr th a').closest("tr").next("tr").find(".nowrap").closest("td").each(function(){console.log($(this).text())})


30 September 1987– 16 April 1992
4 May 1986– 16 April 1992
11 January 1980– 21 November 1985

In [120]:
def clean_line(line):
    # Take out extra space, underscores, comments, etc.
    cleaned = re.sub("_* .+", "", line).strip()
    cleaned = re.sub("_$", "", cleaned, flags=re.MULTILINE)
    return cleaned

def ingest_dictionary(dict_path):
    """
    Read in the country (or other) actor dictionaries.
    """
    with open(dict_path) as f:
        country_file = f.read()
    split_file = country_file.split("\n")
    
    dict_dict = []
    key_name = ""
    alt_names = [] 
    roles = []

    for line in split_file:
        if not line:
            pass
        elif line[0] == "#":
            pass
        elif re.match("[A-Z]", line[0]):
            # handle the previous
            entry = {"actor_en" : key_name,
                    "alt_names_en" : alt_names,
                    "roles" : roles}
            dict_dict.append(entry)
            # zero everything out
            alt_names = []
            roles = []
            # make new key name
            key_name = clean_line(line)
            # check to see if the role is built in
            if bool(re.search("\[[A-Z]{3}\]", line)):
                roles = re.findall("\[(.+?)\]", line)
        elif line[0] == "+":
            cleaned = clean_line(line[1:])
            alt_names.append(cleaned)
        elif re.match("\s", line):
            roles.append(line.strip())
    return dict_dict 
dp = "./Phoenix.Countries.actors.txt"
dict_dict = ingest_dictionary(dp)

In [122]:
len(dict_dict)


Out[122]:
18390

In [153]:
def BuildTheTransladtionDicEngToAr(dict_dict):
    result=[]
    for dictitem in dict_dict[0:10]:
        if(dictitem["actor_en"]==''):
            continue
        try:
            result.append(hack_wiki(dictitem["actor_en"]))
        except Exception as e:
           #traceback.extract_tb(e)
            print(e)https://en.wikipedia.org/wiki/"+"Mohammad_Najibullah"
    return result

In [149]:
engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)


  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/yan/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-149-4b8968f17e17>", line 1, in <module>
    engToArabicDic=BuildTheTransladtionDicEngToAr(dict_dict)
  File "<ipython-input-148-b7a7b3fd8eb7>", line 7, in BuildTheTransladtionDicEngToAr
    result.append(hack_wiki(dictitem["actor_en"]))
  File "<ipython-input-141-7b7d75452f0b>", line 16, in hack_wiki
    traceback.print_stack()

In [147]:
engToArabicDic


Out[147]:
[{'AFGHANISTAN': ['أفغانستان']},
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None]

In [150]:
dict_dict[3]


Out[150]:
{'actor_en': 'MOHAMMAD_ZAHIR_SHAH',
 'alt_names_en': [],
 'roles': ['[AFGELI 320101-331108]',
  '[AFGGOV 331108-730717]',
  '[AFGELI 730717-070723]']}

In [156]:
hack_wiki('AFGHAN_NATIONAL_SECURITY_FORCES')


'NoneType' object has no attribute 'find'

In [205]:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 
#  def init_driver():
#     driver = webdriver.Firefox()
#     driver.wait = WebDriverWait(driver, 5)
#     return driver
 
 
# def lookup(driver, query):
#     driver.get("http://www.google.com")
#     try:
#         box = driver.wait.until(EC.presence_of_element_located(
#             (By.NAME, "q")))
#         button = driver.wait.until(EC.element_to_be_clickable(
#             (By.NAME, "btnK")))
#         box.send_keys(query)
#         button.click()
#     except TimeoutException:
#         print("Box or Button not found in google.com")

In [230]:
#inorder to make the selenium work,
#we need to download geckodrive for firefox and and put it under 'usr/local/bin'
#and we need to make the driver point to where firefox instaleed.
binary = FirefoxBinary(r'/usr/bin/firefox')
caps = DesiredCapabilities.FIREFOX.copy()
#Set ‘marionette’ browser to True
caps['marionette'] = True
#Launch the Firefox instance by specifying the geckodriver executable path
driver = webdriver.Firefox(firefox_binary=binary,capabilities=caps)
driver.wait = WebDriverWait(driver, 5)

In [233]:
driver.get("https://en.wikipedia.org/wiki/"+"SIBGHATULLAH_MOJADEDI")

In [228]:
# driver.get("http://www.google.com")
# # try:
# #     box = driver.wait.until(EC.presence_of_element_located(
# #         (By.NAME, "q")))
# #     button = driver.wait.until(EC.element_to_be_clickable(
# #         (By.NAME, "btnK")))
# #     box.send_keys("Selenium")
# #     button.click()
# # except TimeoutException:
# #     print("Box or Button not found in google.com")
# # time.sleep(5)
# # driver.quit()
# search = driver.find_element_by_name('q')
# search.send_keys("google search through python")
# search.send_keys(Keys.RETURN) # hit return after you enter search text
# time.sleep(5) # sleep for 5 seconds so you can see the results
# browser.quit()


---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-228-9cb6cdde2fe8> in <module>()
      5     button = driver.wait.until(EC.element_to_be_clickable(
      6         (By.NAME, "btnK")))
----> 7     box.send_keys("Selenium")
      8     button.click()
      9 except TimeoutException:

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    347         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    348                       {'text': "".join(keys_to_typing(value)),
--> 349                        'value': keys_to_typing(value)})
    350 
    351     # RenderedWebElement Items

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    491             params = {}
    492         params['id'] = self._id
--> 493         return self._parent.execute(command, params)
    494 
    495     def find_element(self, by=By.ID, value=None):

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    254         response = self.command_executor.execute(driver_command, params)
    255         if response:
--> 256             self.error_handler.check_response(response)
    257             response['value'] = self._unwrap_value(
    258                 response.get('value', None))

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

WebDriverException: Message: Expected [object Undefined] undefined to be a string

In [238]:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

# browser = webdriver.Firefox()
driver.get('http://www.google.com')

search = driver.find_element_by_name('q')
search.send_keys("google search through python")
search.send_keys(Keys.RETURN) # hit return after you enter search text
time.sleep(5) # sleep for 5 seconds so you can see the results
driver.quit()


---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-238-982fb63975d6> in <module>()
     10 
     11 search = driver.find_element_by_name('q')
---> 12 search.send_keys("google search through python")
     13 search.send_keys(Keys.RETURN) # hit return after you enter search text
     14 time.sleep(5) # sleep for 5 seconds so you can see the results

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    347         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    348                       {'text': "".join(keys_to_typing(value)),
--> 349                        'value': keys_to_typing(value)})
    350 
    351     # RenderedWebElement Items

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    491             params = {}
    492         params['id'] = self._id
--> 493         return self._parent.execute(command, params)
    494 
    495     def find_element(self, by=By.ID, value=None):

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    254         response = self.command_executor.execute(driver_command, params)
    255         if response:
--> 256             self.error_handler.check_response(response)
    257             response['value'] = self._unwrap_value(
    258                 response.get('value', None))

~/eventData/yan-virtualenv/document_cluster/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

WebDriverException: Message: Expected [object Undefined] undefined to be a string

In [ ]:
def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver
 
 
def lookup(driver, query):
    driver.get("http://www.google.com")
    try:
        box = driver.wait.until(EC.presence_of_element_located(
            (By.NAME, "q")))
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "btnK")))
        box.send_keys(query)
        button.click()
    except TimeoutException:
        print("Box or Button not found in google.com")
 
 
if __name__ == "__main__":
    driver = init_driver()
    lookup(driver, "Selenium")
    time.sleep(5)
    driver.quit()

In [ ]: