Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from urllib.request import Request, urlopen
- from bs4 import BeautifulSoup
- import datetime
- import csv
- req = Request('https://www.google.com/search?q=euro+value')
- # add user-agent header to our request, a simple string identifying the browser
- # in this case the user-agent is for Firefox 24, the current ESR
- req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0")
- # query the website and return the html to the variable ‘currency_page’
- currency_page = urlopen(req).read()
- # parse the html using beautiful soap and store in variable `soup`
- soup = BeautifulSoup(currency_page, 'html.parser')
- # Get 'span' WITH 'title' element:
- # https://stackoverflow.com/questions/38133759/how-to-get-text-from-span-tag-in-beautifulsoup
- # WITHOUT ' title'
- # https://stackoverflow.com/questions/35967854/scraping-with-beautifulsoup-object-has-no-attribute
- latest_value = soup.find_all('span')
- # Create list to access elements by numerical ID
- myList = []
- # Populate list with 'span' elements from webpage
- for i in latest_value:
- myList.append(i.text)
- # Access the $ element and save it
- now_value = myList[10]
- # FOR CSV FILE - Date & Time
- now = datetime.datetime.now()
- # Append to CSV
- # http://beancoder.com/csv-files-using-python/
- with open('currency_historical_values.csv','a', newline='') as newFile:
- newFileWriter = csv.writer(newFile)
- newFileWriter.writerow(now_value)
- In [47]: %run "C:/Users/[me]/Documents/0_Personal/Coding/Python/CurrencyTrackingProject.py"
Add Comment
Please, Sign In to add comment