You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<div><h2id="guiapp"><ahref="#guiapp" name="guiapp">#</a>GUI App</h2><div><h4id="runsadesktopappforconvertingweightsfrommetricunitsintopounds">Runs a desktop app for converting weights from metric units into pounds:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install PySimpleGUI</span>
window[<spanclass="hljs-string">'OUTPUT'</span>].update(value=<spanclass="hljs-string">f'<spanclass="hljs-subst">{quantity}</span><spanclass="hljs-subst">{unit}</span> is <spanclass="hljs-subst">{lbs:g}</span> lbs.'</span>)
<div><h2id="scraping"><ahref="#scraping" name="scraping">#</a>Scraping</h2><div><h4id="scrapespythonsurlandlogofromitswikipediapage">Scrapes Python's URL and logo from its Wikipedia page:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install requests beautifulsoup4</span>
2054
2052
<spanclass="hljs-keyword">import</span> requests, bs4, os
<list> = <WebDrv/El>.find_elements(<spanclass="hljs-string">'xpath'</span>, …) <spanclass="hljs-comment"># '//<tag>[@<attr>="<val>"]…'. See XPath.</span>
2076
-
<str> = <El>.get_attribute(<str>) <spanclass="hljs-comment"># Property if exists. Also <El>.text.</span>
2077
-
<El>.click/clear() <spanclass="hljs-comment"># Also <El>.send_keys(<str>).</span>
2078
2069
</code></pre></div>
2079
2070
2080
2071
2081
-
<div><h4id="xpathalsoavailableinlxmlscrapyandbrowsersconsoleviadxxpath">XPath — also available in lxml, Scrapy, and browser's console via <codeclass="python hljs"><spanclass="hljs-string">'$x("<xpath>")'</span></code>:</h4><pre><codeclass="python language-python hljs"><xpath> = //<element>[/ <spanclass="hljs-keyword">or</span> // <element>] <spanclass="hljs-comment"># /<child>, //<descendant>, /../<sibling></span>
2082
-
<xpath> = //<element>/following::<element> <spanclass="hljs-comment"># Next element. Also preceding/parent/…</span>
<condition> = [<sub_cond> [<spanclass="hljs-keyword">and</span>/<spanclass="hljs-keyword">or</span> <sub_cond>]] <spanclass="hljs-comment"># For negation use `not(<sub_cond>)`.</span>
2085
-
<sub_cond> = @<attr>[=<spanclass="hljs-string">"<val>"</span>] <spanclass="hljs-comment"># `text()=`, `.=` match (complete) text.</span>
2086
-
<sub_cond> = contains(@<attr>, <spanclass="hljs-string">"<val>"</span>) <spanclass="hljs-comment"># Is <val> a substring of attr's value?</span>
2087
-
<sub_cond> = [//]<element> <spanclass="hljs-comment"># Has matching child? Descendant if //.</span>
2072
+
<pre><codeclass="python language-python hljs"><Drv> = webdriver.Chrome/Firefox/Safari/Edge() <spanclass="hljs-comment"># Opens the browser. Also <Driver>.quit().</span>
2073
+
<Drv>.implicitly_wait(seconds) <spanclass="hljs-comment"># Sets timeout for find_element/s() methods.</span>
2074
+
<Drv>.get(<spanclass="hljs-string">'<url>'</span>) <spanclass="hljs-comment"># Blocks until browser fires the load event.</span>
2075
+
<str> = <Drv>.page_source <spanclass="hljs-comment"># Returns HTML of the page's current state.</span>
<str> = <El>.get_attribute(<spanclass="hljs-string">'<name>'</span>) <spanclass="hljs-comment"># Returns attribute or property if exists.</span>
2078
+
<El>.click/clear() <spanclass="hljs-comment"># Also <El>.text and <El>.send_keys(<str>).</span>
2079
+
</code></pre>
2080
+
<div><h4id="xpathalsoavailableinlxmlscrapyandbrowsersconsoleviadxxpath">XPath — also available in lxml, Scrapy, and browser's console via <codeclass="python hljs"><spanclass="hljs-string">'$x("<xpath>")'</span></code>:</h4><pre><codeclass="python language-python hljs"><xpath> = //<element>[/ <spanclass="hljs-keyword">or</span> // <element>] <spanclass="hljs-comment"># E.g. …/child, …//descendant, …/../sibling.</span>
2081
+
<xpath> = //<element>/following::<element> <spanclass="hljs-comment"># Next element. Also preceding::, parent::.</span>
2082
+
<element> = <tag><conditions><index> <spanclass="hljs-comment"># Tag accepts */a/…. Use [1/2/…] for index.</span>
2083
+
<condition> = [<sub_cond> [<spanclass="hljs-keyword">and</span>/<spanclass="hljs-keyword">or</span> <sub_cond>]] <spanclass="hljs-comment"># Use not(<sub_cond>) to negate condition.</span>
2084
+
<sub_cond> = @<attr>[=<spanclass="hljs-string">"<val>"</span>] <spanclass="hljs-comment"># `text()=` and `.=` match (complete) text.</span>
2085
+
<sub_cond> = contains(@<attr>, <spanclass="hljs-string">"<val>"</span>) <spanclass="hljs-comment"># Is <val> a substring of attribute's value?</span>
2086
+
<sub_cond> = [//]<element> <spanclass="hljs-comment"># Has matching child? Descendant if //<el>.</span>
2088
2087
</code></pre></div>
2089
2088
2090
2089
<div><h2id="webapp"><ahref="#webapp" name="webapp">#</a>Web App</h2><p><strong>Flask is a micro web framework/server. If you just want to open a html file in a web browser use <codeclass="python hljs"><spanclass="hljs-string">'webbrowser.open(<path>)'</span></code> instead.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install flask</span>
0 commit comments