(Mar-26-2018, 11:39 AM)nutgut Wrote: Would be fun to hear what others think about the solution.The solution is okay,but he dos a lot error checking that can be confusing.
It it's simplest form,here download a CSV from web.
Always use Requests and not urllib.
import requests url = 'http://www.sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv' url_get = requests.get(url) # Download csv with open('sample.csv', 'wb') as f_out: f_out.write(url_get.content)Example parse out that link from the website,and the use it.
import requests from bs4 import BeautifulSoup url_csv = 'http://www.sample-videos.com/download-sample-csv.php' url = requests.get(url_csv) soup = BeautifulSoup(url.content, 'lxml') h1 = soup.find('h1') print(h1.text) print('------------') site = soup.find('a', class_="navbar-brand") link = soup.find('a', class_="download_csv") adress_csv = f"{site.get('href')}/{link.get('href')}" print(adress_csv) # Download csv download_link = requests.get(adress_csv) csv_url_name = adress_csv.split('/')[-1] print(csv_url_name) with open(csv_url_name, 'wb') as f_out: f_out.write(download_link.content)
Output:Download Sample CSV
------------
http://www.sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv
Sample-Spreadsheet-10-rows.csv