\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{leenmajz} \pdfinfo{ /Title (files-and-internet-scrapping-python.pdf) /Creator (Cheatography) /Author (leenmajz) /Subject (Files and Internet Scrapping Python Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{4155A3} \definecolor{LightBackground}{HTML}{F3F4F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Files and Internet Scrapping Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{leenmajz} via \textcolor{DarkBackground}{\uline{cheatography.com/198109/cs/41876/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}leenmajz \\ \uline{cheatography.com/leenmajz} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 15th March, 2024.\\ Updated 15th March, 2024.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Unicode}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Code point higher than 127}} \{\{ac\}\} \newline % Row Count 1 (+ 1) "'\textbackslash{}u0915'" or "क"% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{File Modes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{read} \tn % Row Count 1 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{write} \tn % Row Count 2 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{create} \tn % Row Count 3 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{truncate} \tn % Row Count 4 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{position at start} \tn % Row Count 5 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{position at end} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Reading a File}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Opens and closing a file}} \{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{file = open(file\_name, encoding = \{\{colspan=2\}\}'utf-8', 'r')} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{with open(file\_name, "r") as file:\{\{colspan=2\}\}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{file.close()\{\{colspan=2\}\}} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Reading a file}}\{\{colspan=2\}\}} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} Reads entire file as a string & \seqsplit{\_\_\_content=file.read()} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} Reads the first 10 charcters & \_\_\_content = file.read(10) \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} Stores contents as a list of lines & \_\_\_file.readlines() \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} Read next line as a string & \_\_\_file.readline() \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Iterating through lines}}\{\{colspan=2\}\}} \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} & for line in lines: \tn % Row Count 16 (+ 1) % Row 11 \SetRowColor{white} strip to remove newline charc & \seqsplit{\_\_\_print(line.strip())} \tn % Row Count 18 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Writing to a text file}}\{\{colspan=2\}\}} \tn % Row Count 19 (+ 1) % Row 13 \SetRowColor{white} & with open(file\_name, 'w') as file: \tn % Row Count 21 (+ 2) % Row 14 \SetRowColor{LightBackground} writing one line & \seqsplit{\_\_\_file.write("Hello}, World!\textbackslash{}n") \tn % Row Count 23 (+ 2) % Row 15 \SetRowColor{white} writing list of lines & \seqsplit{\_\_\_file.writelines(list)} \tn % Row Count 25 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Appending to a text file}}\{\{colspan=2\}\}} \tn % Row Count 26 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{with open(file\_name, 'a') as file: \{\{colspan=2\}\}} \tn % Row Count 27 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\_\_\_file.write("\textbackslash{}nAppending a new line.") \{\{colspan=2\}\}} \tn % Row Count 29 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Checking file existence}}\{\{colspan=2\}\}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Reading a File (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import os \{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{os.path.exists(file\_name)} \{\{colspan=2\}\}} \tn % Row Count 2 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Handling file exceptions}}\{\{colspan=2\}\}} \tn % Row Count 3 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{try:} \tn % Row Count 4 (+ 1) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\_\_\_with open(file) as f:\{\{colspan=2\}\}} \tn % Row Count 5 (+ 1) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\_\_\_\_\_content = f.read()\{\{colspan=2\}\}} \tn % Row Count 6 (+ 1) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\_\_\_print(content)\{\{colspan=2\}\}} \tn % Row Count 7 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{except FileNotFoundError:\{\{colspan=2\}\}} \tn % Row Count 8 (+ 1) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\_\_\_print(""File not found") \{\{colspan=2\}\}} \tn % Row Count 9 (+ 1) % Row 29 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{except Exception as e: \{\{colspan=2\}\}} \tn % Row Count 10 (+ 1) % Row 30 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\_\_\_print(f"An error occurred: \{e\}") \{\{colspan=2\}\}} \tn % Row Count 11 (+ 1) % Row 31 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\} {\bf{Telling/seeking in a file}}\{\{colspan=2\}\}} \tn % Row Count 12 (+ 1) % Row 32 \SetRowColor{LightBackground} & with open(file) as f: \tn % Row Count 14 (+ 2) % Row 33 \SetRowColor{white} Gives current position & \_\_\_f.tell() \tn % Row Count 16 (+ 2) % Row 34 \SetRowColor{LightBackground} Move the cursor to the beginning & \_\_\_f.seek(0) \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{csv Library}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import csv \{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{colspan=2\}\}\{\{ac\}\} {\bf{Reading CSV file}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} returns a list & content = csv.reader(file, delimiter = ";") \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} & data = list(content) \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{colspan=2\}\}\{\{ac\}\} {\bf{Writing to CSV file}}} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} & writer = csv.writer(file) \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} & writer.writerows(list) \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Loading JSONs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import json} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Loading json file}} \{\{ac\}\}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{json\_data = json.loads(json\_file)} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Change json to string}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{string = json.dumps(data, indent=2)} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.93643 cm} x{2.04057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Random library}} \tn % Row 0 \SetRowColor{LightBackground} & import random \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Returns a random float in the range{[}0,0,1) & random.random() \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Return a random float in the range{[}a,b{]} & \seqsplit{random.uniform(a},b) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Returns an integer in the range {[}0,b) & \seqsplit{random.randrange(b)} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Returns an integer in the range {[}a,b) skipping c steps & \seqsplit{random.randrange(a},b,c) \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} Returns an integer in the range {[}a,b{]} & \seqsplit{random.randint(a},b) \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} Randomly change position of elements & print \seqsplit{(random.shuffle(luck))} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Random Choices}} \tn % Row 0 \SetRowColor{LightBackground} Uniformly randomly picks one item from a list & random.choice(list) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} & list{[}randrange(4){]} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} Selects k items without repetition & random.sample(list, k=2) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Selects k items with repetition & random.choices(list, k=2) \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} Selects k items without uniformity may repeat & random.choices(list, weights={[}10,150,20{]}, k =2) \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{These methods also work on strings} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Raw Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{colspan=2\}\}\{\{ac\}\}{\bf{Raw Strings}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} r-strings & r"A raw string" \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{only a single backslash not valid \{\{colspan=2\}\}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{odd number of ending backslash not valid \{\{colspan=2\}\}} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Regular Expressions- Patterns}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/nevin1_1703803634_Screenshot 2023-12-28 234116.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.64701 cm} x{4.32999 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Regular - Expression Patterns (continued)}} \tn % Row 0 \SetRowColor{LightBackground} * & Match its preceding element zero or more times. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} + & Match its preceding element one or more times. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} ? & Match its preceding element zero or one time. \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \{n\} & Match its preceding element exactly n times. \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \{n ,m\} & Match its preceding element from n to m times. \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{re Methods}} \tn % Row 0 \SetRowColor{LightBackground} Searches the string for a match and returns a Match object & re.match(pattern, string) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Searches for the first occurrence of the pattern anywhere in the string & re.search(pattern,string) \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} Finds all occurrences of the pattern in the string returns a list & \seqsplit{re.findall(pattern},string) \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} Returns an iterator yielding match objects for all matches. & \seqsplit{re.finditer(pattern}, string) \tn % Row Count 14 (+ 3) % Row 4 \SetRowColor{LightBackground} Replaces matching substrings with new string for all occurrences or a specified number & re.sub(pattern, replacement, string) \tn % Row Count 19 (+ 5) % Row 5 \SetRowColor{white} Splits the string where there is a match and returns list of strings based on splits & re.split(pattern, string) \tn % Row Count 23 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Get requests using requests}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import requests \newline url = \seqsplit{"https://www.wikipedia.org/"} \newline r = requests.get(url) \newline text = r.text} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Webscrapping}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{from bs4 import BeautifulSoup \newline \newline \# Parse HTML stored as a string \newline \# 'html5lib' 'html.parser' or 'lxml' \newline soup = BeautifulSoup(html, 'html5lib') \newline \newline \# Returns formatted html \newline soup.prettify() \newline \newline \# Find the first instance of an HTML tag \newline soup.find(tag, attrs=\{"class":"\_\_"\}) \newline \newline \# Find all instances of an HTML tag \newline soup.find\_all(tag, attrs=\{"class":"\_\_"\})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Get requests using urllib}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{from urllib.request import urlopen, Request \newline url = \seqsplit{"https://www.wikipedia.org/"} \newline request = Request(url) \newline response = urlopen(request) \newline html = response.read() \newline response.close()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Higher Order Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# min/max \newline \newline max(iterable{[}, default=obj, key=func{]}) \newline min(iterable{[}, default=obj, key=func{]}) \newline \newline a = min({[}12,"apple",223,"A","B"{]},key= lambda c: len(str(c))) \newline \# Output: "A" (minimum value in the list based on len(str) of the list object) \newline \newline \newline students = {[}\{"name":"Saint", "age":"25"\}, \newline \{"name":"Watson", "age":"35"\}, \newline \{"name":"Karlson", "age":"21"\}, \newline \{"name":"Kenzo", "age":"15"\}{]} \newline \newline youngest= min(students, key= lambda x:x{[}"age"{]} ) \# Output: \{"name":"Kenzo", "age":"15"\}{]} \newline oldest = max(students, key= lambda x:x{[}"age"{]} ) \# Output: \{"name":"Watson", "age":"35"\} \newline \newline \newline \newline \#sorted \newline \newline sorted(iterable, key=func, reverse=reverse) \newline \newline L ={[}"apple", "ban", "dog", "aeroplane"{]} \newline print(sorted(L,key=len)) \# Sort based on lenght of string \newline \newline \newline \#map \newline \newline map(function, iterable, ...) \newline \newline applies a function to the iterable and returns a mapped object \newline \newline Use print(list(map)) to print the value! \newline \newline \# A function to return the square of n \newline def addition(n): \newline return n**2 \newline \newline \# Some iterable \newline list = {[}1,2,3,4{]} \newline \newline \#map the function with the iterable and apply list to the map object \newline \newline print(list(map(addition,list))) \# Output: {[}1,4,9,16{]} \newline \newline \newline \# Filter \newline \newline map(function, iterable, ...) \newline \newline The filter runs through each element of iterable and applies function to it. \newline It filters out list elements for which function doesnt give a True value \newline \newline \newline seq = {[}0, 1, 2, 3, 5, 8, 13{]} \newline \newline \# result filters out non odd numbers \newline result = filter(lambda x: x \% 2 != 0, seq) \newline print(list(result)) \# Output: {[}1,3,5,13{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}