diff --git a/01_Billboard.ipynb b/01_Billboard.ipynb
index 779a38c..17029be 100644
--- a/01_Billboard.ipynb
+++ b/01_Billboard.ipynb
@@ -41,7 +41,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
"id": "promotional-algorithm",
"metadata": {},
"outputs": [],
@@ -59,11 +59,24 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 3,
"id": "civic-broad",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
+ "response = requests.get(url)\n",
+ "response.status_code\n",
"# 200 status code means OK!"
]
},
@@ -77,11 +90,13 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 4,
"id": "revised-digest",
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
},
{
"cell_type": "markdown",
@@ -93,20 +108,25 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 5,
"id": "falling-chambers",
"metadata": {},
"outputs": [],
"source": [
"# song titles\n",
+ "songs = soup.find_all(\"span\", class_=\"chart-element__information__song text--truncate color--primary\")\n",
"\n",
"# artists\n",
+ "artists = soup.find_all(\"span\", class_=\"chart-element__information__artist text--truncate color--secondary\")\n",
"\n",
"# last week\n",
+ "last_week_ranks = soup.find_all(\"span\", class_= \"chart-element__meta text--center color--secondary text--last\")\n",
"\n",
"# peak rank\n",
+ "peak_ranks = soup.find_all(\"span\", class_= \"chart-element__meta text--center color--secondary text--peak\")\n",
"\n",
- "# weeks on chart\n"
+ "# weeks on chart\n",
+ "weeks_on_chart = soup.find_all(\"span\", class_= \"chart-element__meta text--center color--secondary text--week\")"
]
},
{
@@ -119,11 +139,15 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 6,
"id": "amateur-protocol",
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "for i in [songs, artists, last_week_ranks, peak_ranks, weeks_on_chart]:\n",
+ " for j in range(len(i)):\n",
+ " i[j] = i[j].getText()"
+ ]
},
{
"cell_type": "markdown",
@@ -135,11 +159,326 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 7,
"id": "external-instrumentation",
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "billboard = pd.DataFrame(\n",
+ " {\"song\": songs,\n",
+ " \"artist\": artists,\n",
+ " \"last_week_rank\": last_week_ranks,\n",
+ " \"peak_rank\": peak_ranks,\n",
+ " \"weeks_on_chart\": weeks_on_chart}\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "80063bef",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " song | \n",
+ " artist | \n",
+ " last_week_rank | \n",
+ " peak_rank | \n",
+ " weeks_on_chart | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " Industry Baby | \n",
+ " Lil Nas X & Jack Harlow | \n",
+ " 2 | \n",
+ " 1 | \n",
+ " 12 | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Stay | \n",
+ " The Kid LAROI & Justin Bieber | \n",
+ " 1 | \n",
+ " 1 | \n",
+ " 14 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Fancy Like | \n",
+ " Walker Hayes | \n",
+ " 3 | \n",
+ " 3 | \n",
+ " 17 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Bad Habits | \n",
+ " Ed Sheeran | \n",
+ " 5 | \n",
+ " 2 | \n",
+ " 16 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Way 2 Sexy | \n",
+ " Drake Featuring Future & Young Thug | \n",
+ " 4 | \n",
+ " 1 | \n",
+ " 6 | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 95 | \n",
+ " Ain't Shit | \n",
+ " Doja Cat | \n",
+ " - | \n",
+ " 24 | \n",
+ " 13 | \n",
+ "
\n",
+ " \n",
+ " | 96 | \n",
+ " Just About Over You | \n",
+ " Priscilla Block | \n",
+ " - | \n",
+ " 97 | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | 97 | \n",
+ " For Tonight | \n",
+ " Giveon | \n",
+ " - | \n",
+ " 91 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 98 | \n",
+ " Praise God | \n",
+ " Kanye West | \n",
+ " - | \n",
+ " 20 | \n",
+ " 3 | \n",
+ "
\n",
+ " \n",
+ " | 99 | \n",
+ " Bad Morning | \n",
+ " YoungBoy Never Broke Again | \n",
+ " 81 | \n",
+ " 28 | \n",
+ " 3 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
100 rows × 5 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " song artist last_week_rank \\\n",
+ "0 Industry Baby Lil Nas X & Jack Harlow 2 \n",
+ "1 Stay The Kid LAROI & Justin Bieber 1 \n",
+ "2 Fancy Like Walker Hayes 3 \n",
+ "3 Bad Habits Ed Sheeran 5 \n",
+ "4 Way 2 Sexy Drake Featuring Future & Young Thug 4 \n",
+ ".. ... ... ... \n",
+ "95 Ain't Shit Doja Cat - \n",
+ "96 Just About Over You Priscilla Block - \n",
+ "97 For Tonight Giveon - \n",
+ "98 Praise God Kanye West - \n",
+ "99 Bad Morning YoungBoy Never Broke Again 81 \n",
+ "\n",
+ " peak_rank weeks_on_chart \n",
+ "0 1 12 \n",
+ "1 1 14 \n",
+ "2 3 17 \n",
+ "3 2 16 \n",
+ "4 1 6 \n",
+ ".. ... ... \n",
+ "95 24 13 \n",
+ "96 97 1 \n",
+ "97 91 2 \n",
+ "98 20 3 \n",
+ "99 28 3 \n",
+ "\n",
+ "[100 rows x 5 columns]"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "billboard"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1ea579c6",
+ "metadata": {},
+ "source": [
+ "## Recommening a new song"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "d3197368",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "What song do you like? Stay\n"
+ ]
+ }
+ ],
+ "source": [
+ "input_song = input(\"What song do you like? \")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 92,
+ "id": "8cf648b3",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'Moon'"
+ ]
+ },
+ "execution_count": 92,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "input_song"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "28aa8d62",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " song | \n",
+ " artist | \n",
+ " last_week_rank | \n",
+ " peak_rank | \n",
+ " weeks_on_chart | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " Stay | \n",
+ " The Kid LAROI & Justin Bieber | \n",
+ " 1 | \n",
+ " 1 | \n",
+ " 14 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " song artist last_week_rank peak_rank weeks_on_chart\n",
+ "1 Stay The Kid LAROI & Justin Bieber 1 1 14"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "billboard.loc[billboard['song'] == input_song]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "id": "ff25923e",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Recommendation: Fancy Like\n"
+ ]
+ }
+ ],
+ "source": [
+ "pos = -1 #position\n",
+ "for index, song in enumerate(billboard['song'].tolist()):\n",
+ " if song == input_song:\n",
+ " pos = index\n",
+ "\n",
+ "edge = len(billboard) - 1\n",
+ "if pos >= 0:\n",
+ " if pos <= edge - 1:\n",
+ " print('Recommendation:', billboard['song'].iloc[pos + 1])\n",
+ " else:\n",
+ " print('Recommendation:', billboard['song'].iloc[edge - pos])\n",
+ "else:\n",
+ " print('This song is not in the list')"
+ ]
}
],
"metadata": {
@@ -158,7 +497,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.8.5"
+ "version": "3.8.8"
},
"toc": {
"base_numbering": 1,
diff --git a/02_Further_questions.ipynb b/02_Further_questions.ipynb
index 66fb224..9105692 100644
--- a/02_Further_questions.ipynb
+++ b/02_Further_questions.ipynb
@@ -19,6 +19,19 @@
"As you've seen, scraping the internet is a skill that can get you all sorts of information. Here are some little challenges to gain more experience in the field"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 218,
+ "id": "ebcadb1b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from bs4 import BeautifulSoup\n",
+ "import requests\n",
+ "import pandas as pd\n",
+ "import dateutil.parser as dparser"
+ ]
+ },
{
"cell_type": "markdown",
"id": "express-introduction",
@@ -29,7 +42,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
"id": "appreciated-bubble",
"metadata": {},
"outputs": [],
@@ -37,6 +50,203 @@
"url ='https://en.wikipedia.org/wiki/Python'"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "ed026649",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "cf8b4107",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "id": "318eca02",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Computing',\n",
+ " 'People',\n",
+ " 'Roller coasters',\n",
+ " 'Vehicles',\n",
+ " 'Weaponry',\n",
+ " 'Other uses',\n",
+ " 'See also']"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "contents = []\n",
+ "pre = soup.find_all('span', attrs={'class': 'toctext'})\n",
+ "for c in pre:\n",
+ " contents.append(c.get_text())\n",
+ "contents"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "id": "bc17895c",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'https://en.wikipedia.org/wiki/Python#Computing'"
+ ]
+ },
+ "execution_count": 28,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "links = 'https://en.wikipedia.org/wiki/Python' + soup.find('li', attrs={'class': 'toclevel-1 tocsection-1'}).find('a').get('href')\n",
+ "links"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 54,
+ "id": "854fb881",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "contents = []\n",
+ "links = []\n",
+ "\n",
+ "pre = soup.find_all('span', attrs={'class': 'toctext'})\n",
+ "\n",
+ "for i, c in enumerate(pre):\n",
+ " contents.append(c.get_text())\n",
+ " x = i+1\n",
+ " links.append('https://en.wikipedia.org/wiki/Python' + soup.find('li', attrs={'class': 'toclevel-1 tocsection-'+str(x)}).find('a').get('href'))\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 55,
+ "id": "9204697d",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " content | \n",
+ " link | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " Computing | \n",
+ " https://en.wikipedia.org/wiki/Python#Computing | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " People | \n",
+ " https://en.wikipedia.org/wiki/Python#People | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Roller coasters | \n",
+ " https://en.wikipedia.org/wiki/Python#Roller_co... | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Vehicles | \n",
+ " https://en.wikipedia.org/wiki/Python#Vehicles | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Weaponry | \n",
+ " https://en.wikipedia.org/wiki/Python#Weaponry | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " Other uses | \n",
+ " https://en.wikipedia.org/wiki/Python#Other_uses | \n",
+ "
\n",
+ " \n",
+ " | 6 | \n",
+ " See also | \n",
+ " https://en.wikipedia.org/wiki/Python#See_also | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " content link\n",
+ "0 Computing https://en.wikipedia.org/wiki/Python#Computing\n",
+ "1 People https://en.wikipedia.org/wiki/Python#People\n",
+ "2 Roller coasters https://en.wikipedia.org/wiki/Python#Roller_co...\n",
+ "3 Vehicles https://en.wikipedia.org/wiki/Python#Vehicles\n",
+ "4 Weaponry https://en.wikipedia.org/wiki/Python#Weaponry\n",
+ "5 Other uses https://en.wikipedia.org/wiki/Python#Other_uses\n",
+ "6 See also https://en.wikipedia.org/wiki/Python#See_also"
+ ]
+ },
+ "execution_count": 55,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "dct = {'content': contents, 'link': links}\n",
+ "\n",
+ "df = pd.DataFrame.from_dict(dct)\n",
+ "df"
+ ]
+ },
{
"cell_type": "markdown",
"id": "relevant-performer",
@@ -47,7 +257,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 56,
"id": "scenic-surgeon",
"metadata": {},
"outputs": [],
@@ -55,6 +265,60 @@
"url = 'http://uscode.house.gov/download/download.shtml'"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 57,
+ "id": "e8904fdc",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 57,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 58,
+ "id": "43e37d27",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 80,
+ "id": "d65d5e18",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Number of titles: 54\n"
+ ]
+ }
+ ],
+ "source": [
+ "titles = []\n",
+ "pre = soup.find_all('div', attrs={'class': 'usctitle'})\n",
+ "for t in pre:\n",
+ " titles.append(t.get_text())\n",
+ "print('Number of titles:', titles[-1].split()[1])"
+ ]
+ },
{
"cell_type": "markdown",
"id": "acute-necessity",
@@ -65,12 +329,229 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 103,
"id": "starting-blackberry",
"metadata": {},
"outputs": [],
"source": [
- "url = 'https://www.fbi.gov/wanted/topten"
+ "url = 'https://www.fbi.gov/wanted/topten'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 104,
+ "id": "f5138323",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 104,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 105,
+ "id": "0c5ab86d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 151,
+ "id": "b29f2f4f",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['arnoldo jimenez',\n",
+ " 'jason derek brown',\n",
+ " 'alexis flores',\n",
+ " 'jose rodolfo villarreal hernandez',\n",
+ " 'octaviano juarez corro',\n",
+ " 'eugene palmer',\n",
+ " 'rafael caro quintero',\n",
+ " 'bhadreshkumar chetanbhai patel',\n",
+ " 'alejandro castillo',\n",
+ " 'robert william fisher']"
+ ]
+ },
+ "execution_count": 151,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "wanted = []\n",
+ "pre = soup.find_all('li', attrs={'class': 'portal-type-person castle-grid-block-item'})\n",
+ "\n",
+ "for i, c in enumerate(pre):\n",
+ " wanted.append(c.find('a').get('href').split('/')[-1].replace('-',' '))\n",
+ " x = i+1\n",
+ " \n",
+ "wanted"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 149,
+ "id": "8b14afed",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Arnoldo Jimenez',\n",
+ " 'Jason Derek Brown',\n",
+ " 'Alexis Flores',\n",
+ " 'Jose Rodolfo Villarreal Hernandez',\n",
+ " 'Octaviano Juarez Corro',\n",
+ " 'Eugene Palmer',\n",
+ " 'Rafael Caro Quintero',\n",
+ " 'Bhadreshkumar Chetanbhai Patel',\n",
+ " 'Alejandro Castillo',\n",
+ " 'Robert William Fisher']"
+ ]
+ },
+ "execution_count": 149,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "temp = []\n",
+ "temp2 = []\n",
+ "temp3 = []\n",
+ "\n",
+ "for w in wanted:\n",
+ " w = w.split(' ')\n",
+ " temp.append(w)\n",
+ "\n",
+ "for a in temp:\n",
+ " newa = []\n",
+ " for b in a:\n",
+ " b = b.capitalize()\n",
+ " newa.append(b)\n",
+ " temp2.append(newa)\n",
+ " \n",
+ "for item in temp2:\n",
+ " t = ' '.join(item)\n",
+ " temp3.append(t)\n",
+ " \n",
+ "temp3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 161,
+ "id": "dab0cd97",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " Arnoldo Jimenez | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Jason Derek Brown | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Alexis Flores | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Jose Rodolfo Villarreal Hernandez | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " Octaviano Juarez Corro | \n",
+ "
\n",
+ " \n",
+ " | 6 | \n",
+ " Eugene Palmer | \n",
+ "
\n",
+ " \n",
+ " | 7 | \n",
+ " Rafael Caro Quintero | \n",
+ "
\n",
+ " \n",
+ " | 8 | \n",
+ " Bhadreshkumar Chetanbhai Patel | \n",
+ "
\n",
+ " \n",
+ " | 9 | \n",
+ " Alejandro Castillo | \n",
+ "
\n",
+ " \n",
+ " | 10 | \n",
+ " Robert William Fisher | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Name\n",
+ "1 Arnoldo Jimenez\n",
+ "2 Jason Derek Brown\n",
+ "3 Alexis Flores\n",
+ "4 Jose Rodolfo Villarreal Hernandez\n",
+ "5 Octaviano Juarez Corro\n",
+ "6 Eugene Palmer\n",
+ "7 Rafael Caro Quintero\n",
+ "8 Bhadreshkumar Chetanbhai Patel\n",
+ "9 Alejandro Castillo\n",
+ "10 Robert William Fisher"
+ ]
+ },
+ "execution_count": 161,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "topwanted = pd.DataFrame(temp3, index = list(range(1,11)), columns = ['Name'])\n",
+ "topwanted"
]
},
{
@@ -83,7 +564,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 162,
"id": "copyrighted-taiwan",
"metadata": {},
"outputs": [],
@@ -91,6 +572,392 @@
"url = 'https://www.emsc-csem.org/Earthquake/'"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 163,
+ "id": "be4ff9bb",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 163,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 164,
+ "id": "cb40a40f",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 251,
+ "id": "d4529b73",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Date', 'Time', 'Latitude degrees', 'Longitude degrees', 'Region name']"
+ ]
+ },
+ "execution_count": 251,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "columns = []\n",
+ "\n",
+ "header = soup.find_all('th', attrs={'class': 'th2'})\n",
+ "for d in header:\n",
+ " columns.append(d.get_text())\n",
+ "\n",
+ "columns = ['Date', 'Time', columns[2], columns[3], columns[6][:11]]\n",
+ "\n",
+ "columns"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 219,
+ "id": "4cc71690",
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": [
+ "date_time = []\n",
+ "\n",
+ "dates = soup.find_all('td', attrs={'class': 'tabev6'})\n",
+ "for d in dates:\n",
+ " date_time.append(d.get_text())\n",
+ "\n",
+ "date = []\n",
+ "time = []\n",
+ "marker = \"hr\"\n",
+ "\n",
+ "for d in date_time:\n",
+ " if marker not in d:\n",
+ " d2 = dparser.parse(d,fuzzy=True)\n",
+ " date.append(str(d2)[:10])\n",
+ " time.append(str(d2)[11:16])\n",
+ " else: \n",
+ " d2 = dparser.parse(d.split(\"hr\")[0],fuzzy=True)\n",
+ " date.append(str(d2)[:10])\n",
+ " time.append(str(d2)[11:16])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 237,
+ "id": "838e55b0",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [],
+ "source": [
+ "latitude = []\n",
+ "longitude = []\n",
+ "\n",
+ "temp4 = soup.find_all('td', attrs={'class': 'tabev1'})\n",
+ "counter = 1\n",
+ "for l in temp4:\n",
+ " if (counter%2 == 0) is True:\n",
+ " longitude.append(l.get_text()[:5])\n",
+ " counter += 1\n",
+ " else:\n",
+ " latitude.append(l.get_text()[:5])\n",
+ " counter += 1"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 241,
+ "id": "08f5f323",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "region = []\n",
+ "\n",
+ "temp5 = soup.find_all('td', attrs={'class': 'tb_region'})\n",
+ "for r in temp5:\n",
+ " region.append(r.get_text().replace('\\xa0',''))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 258,
+ "id": "7b4f01f5",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Date | \n",
+ " Time | \n",
+ " Latitude degrees | \n",
+ " Longitude degrees | \n",
+ " Region name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 2021-10-20 | \n",
+ " 10:00 | \n",
+ " 28.59 | \n",
+ " 17.85 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 2021-10-20 | \n",
+ " 09:58 | \n",
+ " 28.56 | \n",
+ " 17.83 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 2021-10-20 | \n",
+ " 09:55 | \n",
+ " 28.55 | \n",
+ " 17.85 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 2021-10-20 | \n",
+ " 09:54 | \n",
+ " 28.56 | \n",
+ " 17.84 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 2021-10-20 | \n",
+ " 09:46 | \n",
+ " 40.63 | \n",
+ " 122.4 | \n",
+ " NORTHERN CALIFORNIA | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " 2021-10-20 | \n",
+ " 09:44 | \n",
+ " 28.56 | \n",
+ " 17.83 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 6 | \n",
+ " 2021-10-20 | \n",
+ " 09:28 | \n",
+ " 42.42 | \n",
+ " 9.02 | \n",
+ " SPAIN | \n",
+ "
\n",
+ " \n",
+ " | 7 | \n",
+ " 2021-10-20 | \n",
+ " 09:26 | \n",
+ " 40.46 | \n",
+ " 175.8 | \n",
+ " NORTH ISLAND OF NEW ZEALAND | \n",
+ "
\n",
+ " \n",
+ " | 8 | \n",
+ " 2021-10-20 | \n",
+ " 09:26 | \n",
+ " 28.58 | \n",
+ " 17.84 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 9 | \n",
+ " 2021-10-20 | \n",
+ " 09:25 | \n",
+ " 28.56 | \n",
+ " 17.86 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 10 | \n",
+ " 2021-10-20 | \n",
+ " 09:23 | \n",
+ " 28.56 | \n",
+ " 17.87 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 11 | \n",
+ " 2021-10-20 | \n",
+ " 09:21 | \n",
+ " 37.25 | \n",
+ " 20.46 | \n",
+ " IONIAN SEA | \n",
+ "
\n",
+ " \n",
+ " | 12 | \n",
+ " 2021-10-20 | \n",
+ " 09:21 | \n",
+ " 8.26 | \n",
+ " 116.7 | \n",
+ " LOMBOK REGION, INDONESIA | \n",
+ "
\n",
+ " \n",
+ " | 13 | \n",
+ " 2021-10-20 | \n",
+ " 09:20 | \n",
+ " 18.69 | \n",
+ " 69.41 | \n",
+ " TARAPACA, CHILE | \n",
+ "
\n",
+ " \n",
+ " | 14 | \n",
+ " 2021-10-20 | \n",
+ " 09:18 | \n",
+ " 28.57 | \n",
+ " 17.83 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 15 | \n",
+ " 2021-10-20 | \n",
+ " 09:18 | \n",
+ " 47.55 | \n",
+ " 6.56 | \n",
+ " FRANCE | \n",
+ "
\n",
+ " \n",
+ " | 16 | \n",
+ " 2021-10-20 | \n",
+ " 09:09 | \n",
+ " 28.55 | \n",
+ " 17.84 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 17 | \n",
+ " 2021-10-20 | \n",
+ " 09:06 | \n",
+ " 28.56 | \n",
+ " 17.84 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 18 | \n",
+ " 2021-10-20 | \n",
+ " 08:58 | \n",
+ " 28.57 | \n",
+ " 17.83 | \n",
+ " CANARY ISLANDS, SPAIN REGION | \n",
+ "
\n",
+ " \n",
+ " | 19 | \n",
+ " 2021-10-20 | \n",
+ " 08:57 | \n",
+ " 16.41 | \n",
+ " 93.20 | \n",
+ " CHIAPAS, MEXICO | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Date Time Latitude degrees Longitude degrees \\\n",
+ "0 2021-10-20 10:00 28.59 17.85 \n",
+ "1 2021-10-20 09:58 28.56 17.83 \n",
+ "2 2021-10-20 09:55 28.55 17.85 \n",
+ "3 2021-10-20 09:54 28.56 17.84 \n",
+ "4 2021-10-20 09:46 40.63 122.4 \n",
+ "5 2021-10-20 09:44 28.56 17.83 \n",
+ "6 2021-10-20 09:28 42.42 9.02 \n",
+ "7 2021-10-20 09:26 40.46 175.8 \n",
+ "8 2021-10-20 09:26 28.58 17.84 \n",
+ "9 2021-10-20 09:25 28.56 17.86 \n",
+ "10 2021-10-20 09:23 28.56 17.87 \n",
+ "11 2021-10-20 09:21 37.25 20.46 \n",
+ "12 2021-10-20 09:21 8.26 116.7 \n",
+ "13 2021-10-20 09:20 18.69 69.41 \n",
+ "14 2021-10-20 09:18 28.57 17.83 \n",
+ "15 2021-10-20 09:18 47.55 6.56 \n",
+ "16 2021-10-20 09:09 28.55 17.84 \n",
+ "17 2021-10-20 09:06 28.56 17.84 \n",
+ "18 2021-10-20 08:58 28.57 17.83 \n",
+ "19 2021-10-20 08:57 16.41 93.20 \n",
+ "\n",
+ " Region name \n",
+ "0 CANARY ISLANDS, SPAIN REGION \n",
+ "1 CANARY ISLANDS, SPAIN REGION \n",
+ "2 CANARY ISLANDS, SPAIN REGION \n",
+ "3 CANARY ISLANDS, SPAIN REGION \n",
+ "4 NORTHERN CALIFORNIA \n",
+ "5 CANARY ISLANDS, SPAIN REGION \n",
+ "6 SPAIN \n",
+ "7 NORTH ISLAND OF NEW ZEALAND \n",
+ "8 CANARY ISLANDS, SPAIN REGION \n",
+ "9 CANARY ISLANDS, SPAIN REGION \n",
+ "10 CANARY ISLANDS, SPAIN REGION \n",
+ "11 IONIAN SEA \n",
+ "12 LOMBOK REGION, INDONESIA \n",
+ "13 TARAPACA, CHILE \n",
+ "14 CANARY ISLANDS, SPAIN REGION \n",
+ "15 FRANCE \n",
+ "16 CANARY ISLANDS, SPAIN REGION \n",
+ "17 CANARY ISLANDS, SPAIN REGION \n",
+ "18 CANARY ISLANDS, SPAIN REGION \n",
+ "19 CHIAPAS, MEXICO "
+ ]
+ },
+ "execution_count": 258,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "dct = {'Date': date[:20], 'Time': time[:20], 'Latitude degrees': latitude[:20]\n",
+ " , 'Longitude degrees': longitude[:20], 'Region name': region[:20]}\n",
+ "\n",
+ "earthquakes_info = pd.DataFrame.from_dict(dct)\n",
+ "earthquakes_info"
+ ]
+ },
{
"cell_type": "markdown",
"id": "dominican-defeat",
@@ -99,9 +966,17 @@
"### List all language names and number of related articles in the order they appear in [wikipedia.org](wikipedia.org)"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "b58e3972",
+ "metadata": {},
+ "source": [
+ "- I couldn't find the place with the number of related articles"
+ ]
+ },
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 255,
"id": "metric-vertex",
"metadata": {},
"outputs": [],
@@ -109,6 +984,182 @@
"url = 'https://www.wikipedia.org/'"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 256,
+ "id": "866e9676",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 256,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 259,
+ "id": "101cf814",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 277,
+ "id": "8d42443c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "langs = []\n",
+ "temp6 = soup.find_all('select', attrs={'id': 'searchLanguage'})\n",
+ "\n",
+ "for r in temp6:\n",
+ " langs.append(r.get_text().replace('/','').split())\n",
+ " \n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 280,
+ "id": "b229b82a",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Number of languages on Wikipedia: 86\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('Number of languages on Wikipedia:', len(langs[0]))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 289,
+ "id": "c9bc8877",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Languages | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " Afrikaans | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Polski | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Slovenčina | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " العربية | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " Asturianu | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 82 | \n",
+ " Winaray | \n",
+ "
\n",
+ " \n",
+ " | 83 | \n",
+ " 粵語 | \n",
+ "
\n",
+ " \n",
+ " | 84 | \n",
+ " 中文 | \n",
+ "
\n",
+ " \n",
+ " | 85 | \n",
+ " မြန်မာဘာသာ | \n",
+ "
\n",
+ " \n",
+ " | 86 | \n",
+ " 한국어 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
86 rows × 1 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Languages\n",
+ "1 Afrikaans\n",
+ "2 Polski\n",
+ "3 Slovenčina\n",
+ "4 العربية\n",
+ "5 Asturianu\n",
+ ".. ...\n",
+ "82 Winaray\n",
+ "83 粵語\n",
+ "84 中文\n",
+ "85 မြန်မာဘာသာ\n",
+ "86 한국어\n",
+ "\n",
+ "[86 rows x 1 columns]"
+ ]
+ },
+ "execution_count": 289,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "num = len(langs[0])+1\n",
+ "df = pd.DataFrame(langs[0], index = list(range(1,num)), columns = ['Languages'])\n",
+ "df"
+ ]
+ },
{
"cell_type": "markdown",
"id": "split-cartridge",
@@ -119,12 +1170,187 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 292,
"id": "actual-parallel",
"metadata": {},
"outputs": [],
"source": [
- "url = 'https://data.gov.uk/"
+ "url = 'https://data.gov.uk/'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 293,
+ "id": "69fa6622",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 293,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 294,
+ "id": "6f3df064",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 295,
+ "id": "4aac849d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "datasets = []\n",
+ "temp7 = soup.find_all('a', attrs={'class': 'govuk-link'})\n",
+ "\n",
+ "for d in temp7:\n",
+ " datasets.append(d.get_text())"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 301,
+ "id": "0fa73b55",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Business and economy',\n",
+ " 'Crime and justice',\n",
+ " 'Defence',\n",
+ " 'Education',\n",
+ " 'Environment',\n",
+ " 'Government',\n",
+ " 'Government spending',\n",
+ " 'Health',\n",
+ " 'Mapping',\n",
+ " 'Society',\n",
+ " 'Towns and cities',\n",
+ " 'Transport',\n",
+ " 'Digital service performance',\n",
+ " 'Government reference data']"
+ ]
+ },
+ "execution_count": 301,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "datasets = datasets[4:]\n",
+ "datasets"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 298,
+ "id": "3b078678",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "subdatasets = []\n",
+ "temp8 = soup.find_all('p', attrs={'class': 'govuk-body'})\n",
+ "\n",
+ "for d in temp8:\n",
+ " subdatasets.append(d.get_text())"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 302,
+ "id": "be374b22",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Small businesses, industry, imports, exports and trade',\n",
+ " 'Courts, police, prison, offenders, borders and immigration',\n",
+ " 'Armed forces, health and safety, search and rescue',\n",
+ " 'Students, training, qualifications and the National Curriculum',\n",
+ " 'Weather, flooding, rivers, air quality, geology and agriculture',\n",
+ " 'Staff numbers and pay, local councillors and department business plans',\n",
+ " 'Includes all payments by government departments over £25,000',\n",
+ " 'Includes smoking, drugs, alcohol, medicine performance and hospitals',\n",
+ " 'Addresses, boundaries, land ownership, aerial photographs, seabed and land terrain',\n",
+ " 'Employment, benefits, household finances, poverty and population',\n",
+ " 'Includes housing, urban planning, leisure, waste and energy, consumption',\n",
+ " 'Airports, roads, freight, electric vehicles, parking, buses and footpaths',\n",
+ " 'Cost, usage, completion rate, digital take-up, satisfaction',\n",
+ " 'Trusted data that is referenced and shared across government departments']"
+ ]
+ },
+ "execution_count": 302,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "subdatasets = subdatasets[1:]\n",
+ "subdatasets"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 305,
+ "id": "45b748d5",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 305,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "len(datasets) == len(subdatasets)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 310,
+ "id": "106b2812",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "{'Business and economy': 'Small businesses, industry, imports, exports and trade', 'Crime and justice': 'Courts, police, prison, offenders, borders and immigration', 'Defence': 'Armed forces, health and safety, search and rescue', 'Education': 'Students, training, qualifications and the National Curriculum', 'Environment': 'Weather, flooding, rivers, air quality, geology and agriculture', 'Government': 'Staff numbers and pay, local councillors and department business plans', 'Government spending': 'Includes all payments by government departments over £25,000', 'Health': 'Includes smoking, drugs, alcohol, medicine performance and hospitals', 'Mapping': 'Addresses, boundaries, land ownership, aerial photographs, seabed and land terrain', 'Society': 'Employment, benefits, household finances, poverty and population', 'Towns and cities': 'Includes housing, urban planning, leisure, waste and energy, consumption', 'Transport': 'Airports, roads, freight, electric vehicles, parking, buses and footpaths', 'Digital service performance': 'Cost, usage, completion rate, digital take-up, satisfaction', 'Government reference data': 'Trusted data that is referenced and shared across government departments'}\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Get pairs of elements\n",
+ "zip_iterator = zip(datasets, subdatasets)\n",
+ "\n",
+ "#Convert to dictionary\n",
+ "a_dictionary = dict(zip_iterator)\n",
+ "\n",
+ "print(a_dictionary)"
]
},
{
@@ -135,15 +1361,198 @@
"### Display the top 10 languages by number of native speakers stored in a pandas dataframe"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "3cc62660",
+ "metadata": {},
+ "source": [
+ "- On this question the outout I got from the parser was a nested list where I would have all the rows I need. But I couldn't make a script to read it bc in some point the spacement changes. That's why I have the error at the final line. As I'm late with other labs and I think I got the idea of this one I'm leaving it behind."
+ ]
+ },
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 311,
"id": "adaptive-calculator",
"metadata": {},
"outputs": [],
"source": [
"url = 'https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers'"
]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 312,
+ "id": "cd25c104",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "200"
+ ]
+ },
+ "execution_count": 312,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = requests.get(url)\n",
+ "response.status_code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 313,
+ "id": "e999249f",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "soup = BeautifulSoup(response.content, \"html.parser\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 320,
+ "id": "3bfacc39",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "rank = []\n",
+ "language = []\n",
+ "speakers = []\n",
+ "\n",
+ "ranktemp = soup.find_all('tr')\n",
+ "\n",
+ "for r in ranktemp:\n",
+ " rank.append(r.get_text().split('\\n'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 325,
+ "id": "6e766257",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['This section needs to be updated. Please help update this article to reflect recent events or newly available information. (December 2020)']"
+ ]
+ },
+ "execution_count": 325,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "rank[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 324,
+ "id": "5647ed6f",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['See also', 'Lists of languages', 'Category:Languages', '']"
+ ]
+ },
+ "execution_count": 324,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "rank[-1]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 326,
+ "id": "e3ec0a86",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "214"
+ ]
+ },
+ "execution_count": 326,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "len(rank)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 335,
+ "id": "1138fd89",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['',\n",
+ " '91',\n",
+ " '',\n",
+ " 'Sylheti',\n",
+ " '',\n",
+ " '10.3',\n",
+ " '',\n",
+ " '0.134%',\n",
+ " '',\n",
+ " 'Indo-European',\n",
+ " '',\n",
+ " 'Indo-Aryan',\n",
+ " '']"
+ ]
+ },
+ "execution_count": 335,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "rank[92]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 336,
+ "id": "d7a352f3",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "ValueError",
+ "evalue": "1 columns passed, passed data had 13 columns",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36m_list_to_arrays\u001b[1;34m(data, columns, coerce_float, dtype)\u001b[0m\n\u001b[0;32m 567\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 568\u001b[1;33m \u001b[0mcolumns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_validate_or_indexify_columns\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 569\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_convert_object_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcoerce_float\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcoerce_float\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36m_validate_or_indexify_columns\u001b[1;34m(content, columns)\u001b[0m\n\u001b[0;32m 691\u001b[0m \u001b[1;31m# caller's responsibility to check for this...\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 692\u001b[1;33m raise AssertionError(\n\u001b[0m\u001b[0;32m 693\u001b[0m \u001b[1;34mf\"{len(columns)} columns passed, passed data had \"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mAssertionError\u001b[0m: 1 columns passed, passed data had 13 columns",
+ "\nThe above exception was the direct cause of the following exception:\n",
+ "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrank\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m92\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrank\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data, index, columns, dtype, copy)\u001b[0m\n\u001b[0;32m 568\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mis_named_tuple\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mcolumns\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 569\u001b[0m \u001b[0mcolumns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_fields\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 570\u001b[1;33m \u001b[0marrays\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mto_arrays\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 571\u001b[0m \u001b[0mcolumns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mensure_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 572\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36mto_arrays\u001b[1;34m(data, columns, coerce_float, dtype)\u001b[0m\n\u001b[0;32m 526\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# columns if columns is not None else []\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 527\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 528\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0m_list_to_arrays\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcoerce_float\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcoerce_float\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 529\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mabc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMapping\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 530\u001b[0m return _list_of_dict_to_arrays(\n",
+ "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36m_list_to_arrays\u001b[1;34m(data, columns, coerce_float, dtype)\u001b[0m\n\u001b[0;32m 569\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_convert_object_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcoerce_float\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcoerce_float\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 570\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mAssertionError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 571\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 572\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 573\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mValueError\u001b[0m: 1 columns passed, passed data had 13 columns"
+ ]
+ }
+ ],
+ "source": [
+ "df = pd.DataFrame(rank[1:92],columns=rank[0])"
+ ]
}
],
"metadata": {
@@ -162,7 +1571,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.8.5"
+ "version": "3.8.8"
},
"toc": {
"base_numbering": 1,