In [18]:
!pip3 install requests
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: requests in /usr/lib/python3.10/site-packages (2.26.0) Requirement already satisfied: chardet>=3.0.2 in /usr/lib/python3.10/site-packages (from requests) (4.0.0) Requirement already satisfied: idna>=2.5 in /usr/lib/python3.10/site-packages (from requests) (3.3) Requirement already satisfied: urllib3>=1.21.1 in /usr/lib/python3.10/site-packages (from requests) (1.26.7)
In [19]:
url = "https://raw.githubusercontent.com/codebasics/data-structures-algorithms-python/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv"
In [20]:
import requests
r = requests.get(url)
r.status_code
Out[20]:
200
In [21]:
r.text
Out[21]:
'date,temperature(F)\nJan 1,27\nJan 2,31\nJan 3,23\nJan 4,34\nJan 5,37\nJan 6,38\nJan 7,29\nJan 8,30\nJan 9,35\nJan 10,30'
In [22]:
from urllib.parse import urlparse
parse_object = urlparse(url)
print(parse_object)
ParseResult(scheme='https', netloc='raw.githubusercontent.com', path='/codebasics/data-structures-algorithms-python/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv', params='', query='', fragment='')
In [23]:
file_name = parse_object.path.split("/")[-1]
In [24]:
with open(file_name, 'w') as file:
file.write(r.text)
In [25]:
!cat nyc_weather.csv
date,temperature(F) Jan 1,27 Jan 2,31 Jan 3,23 Jan 4,34 Jan 5,37 Jan 6,38 Jan 7,29 Jan 8,30 Jan 9,35 Jan 10,30
In [ ]:
0 comments:
Post a Comment