Commit 9d254642 authored by sashika sewwandi's avatar sashika sewwandi

Upload New File

parent d63690ad
Pipeline #5787 canceled with stages
from __future__ import print_function
import csv
import requests
from bs4 import BeautifulSoup
class ScrapData:
def __init__(self):
self.page_link = None
self.quotes = []
self.zip_file_path = None
self.download_url = None
def create_download_url(self):
try:
self.page_link = "http://www.values.com/inspirational-quotes"
page_response = requests.get(self.page_link, timeout=5)
page_string = page_response.content
# Download latest release
soup = BeautifulSoup(page_string, "html.parser")
r = soup.find('div', attrs={'id': 'all_quotes'})
for row in r.findAll('div', attrs={'class': 'col-6 col-lg-4 text-center margin-30px-bottom '
'sm-margin-30px-top'}):
quote = {'theme': row.h5.text, 'url': row.a['href'], 'img': row.img['src'],
'lines': row.img['alt'].split(" #")[0], 'author': row.img['alt'].split(" #")[1]}
self.quotes.append(quote)
except Exception as e:
print("Can't scrape data from this site:", self.page_link)
print(e)
def download_zip_file(self):
try:
filename = 'quotes.csv'
with open(filename, 'w', newline='') as f:
w = csv.DictWriter(f, ['theme', 'url', 'img', 'lines', 'author'])
w.writeheader()
for quote in self.quotes:
w.writerow(quote)
except Exception as e:
print("file download is unsuccessful")
print(e)
def invoke(self):
self.create_download_url()
self.download_zip_file()
if __name__ == '__main__':
custom_driver_params = 'Create a new file WebScraping inspirational-quotes data'
new_file = ScrapData()
new_file.invoke()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment