Browse Source

Class, Scraping

pull/121/head
Jure Šorn 3 years ago
parent
commit
ed0b0b3094
3 changed files with 12 additions and 11 deletions
  1. 9
      README.md
  2. 13
      index.html
  3. 1
      pdf/remove_links.py

9
README.md

@ -1043,7 +1043,7 @@ class <class_name>:
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set) <attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
``` ```
* **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.** * **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.**
* **For object to be hashable, all attributes must be [hashable](#hashable) and frozen must be True.**
* **For object to be hashable, all attributes must be hashable and 'frozen' must be True.**
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).** * **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).**
* **For attributes of arbitrary type use `'typing.Any'`.** * **For attributes of arbitrary type use `'typing.Any'`.**
@ -2475,7 +2475,7 @@ Scraping
#### Scrapes Python's URL, version number and logo from its Wikipedia page: #### Scrapes Python's URL, version number and logo from its Wikipedia page:
```python ```python
# $ pip3 install requests beautifulsoup4 # $ pip3 install requests beautifulsoup4
import requests, bs4, sys
import requests, bs4, os, sys
WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(programming_language)' WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
try: try:
@ -2486,9 +2486,10 @@ try:
version = table.find('th', text='Stable release').next_sibling.strings.__next__() version = table.find('th', text='Stable release').next_sibling.strings.__next__()
logo_url = table.find('img')['src'] logo_url = table.find('img')['src']
logo = requests.get(f'https:{logo_url}').content logo = requests.get(f'https:{logo_url}').content
with open('test.png', 'wb') as file:
filename = os.path.basename(logo_url)
with open(filename, 'wb') as file:
file.write(logo) file.write(logo)
print(python_url, version)
print(f'{python_url}, {version}, file://{os.path.abspath(filename)}')
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
print("You've got problems with connection.", file=sys.stderr) print("You've got problems with connection.", file=sys.stderr)
``` ```

13
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>January 27, 2022</aside>
<aside>January 29, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -886,7 +886,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<ul> <ul>
<li><strong>Objects can be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li> <li><strong>Objects can be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
<li><strong>For object to be hashable, all attributes must be <a href="#hashable">hashable</a> and frozen must be True.</strong></li>
<li><strong>For object to be hashable, all attributes must be hashable and 'frozen' must be True.</strong></li>
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li> <li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li>
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li> <li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
</ul> </ul>
@ -2023,7 +2023,7 @@ logger.&lt;level&gt;(<span class="hljs-string">'A logging message.'</span>)
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;str&gt;'</span></code> - Max age as a string: <code class="python hljs"><span class="hljs-string">'1 week, 3 days'</span></code>, <code class="python hljs"><span class="hljs-string">'2 months'</span></code>, …</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'&lt;str&gt;'</span></code> - Max age as a string: <code class="python hljs"><span class="hljs-string">'1 week, 3 days'</span></code>, <code class="python hljs"><span class="hljs-string">'2 months'</span></code>, …</strong></li>
</ul> </ul>
<div><h2 id="scraping"><a href="#scraping" name="scraping">#</a>Scraping</h2><div><h4 id="scrapespythonsurlversionnumberandlogofromitswikipediapage">Scrapes Python's URL, version number and logo from its Wikipedia page:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests beautifulsoup4</span> <div><h2 id="scraping"><a href="#scraping" name="scraping">#</a>Scraping</h2><div><h4 id="scrapespythonsurlversionnumberandlogofromitswikipediapage">Scrapes Python's URL, version number and logo from its Wikipedia page:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests beautifulsoup4</span>
<span class="hljs-keyword">import</span> requests, bs4, sys
<span class="hljs-keyword">import</span> requests, bs4, os, sys
WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span> WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span>
<span class="hljs-keyword">try</span>: <span class="hljs-keyword">try</span>:
@ -2034,9 +2034,10 @@ WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(prog
version = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__() version = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__()
logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>] logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>]
logo = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{logo_url}</span>'</span>).content logo = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{logo_url}</span>'</span>).content
<span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.png'</span>, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
filename = os.path.basename(logo_url)
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
file.write(logo) file.write(logo)
print(python_url, version)
print(<span class="hljs-string">f'<span class="hljs-subst">{python_url}</span>, <span class="hljs-subst">{version}</span>, file://<span class="hljs-subst">{os.path.abspath(filename)}</span>'</span>)
<span class="hljs-keyword">except</span> requests.exceptions.ConnectionError: <span class="hljs-keyword">except</span> requests.exceptions.ConnectionError:
print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr) print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr)
</code></pre></div></div> </code></pre></div></div>
@ -2881,7 +2882,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer> <footer>
<aside>January 27, 2022</aside>
<aside>January 29, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

1
pdf/remove_links.py

@ -11,7 +11,6 @@ MATCHES = {
'<strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong>': '<strong>Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.</strong>', '<strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong>': '<strong>Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.</strong>',
'<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its repr() method (p. 14).</strong>', '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its repr() method (p. 14).</strong>',
'<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).</strong>', '<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).</strong>',
'<strong>For object to be hashable, all attributes must be <a href="#hashable">hashable</a> and frozen must be True.</strong>': '<strong>For object to be hashable, all attributes must be hashable (p. 16) and frozen must be True.</strong>',
'<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any <a href="#callable">callable</a>.</strong>': '<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable (p. 17).</strong>', '<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any <a href="#callable">callable</a>.</strong>': '<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable (p. 17).</strong>',
'<strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p.&nbsp;3).</strong>', '<strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p.&nbsp;3).</strong>',
'<strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong>': '<strong>Objects returned by the itertools module, such as count, repeat and cycle (p. 3).</strong>', '<strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong>': '<strong>Objects returned by the itertools module, such as count, repeat and cycle (p. 3).</strong>',

Loading…
Cancel
Save