Guest User

Untitled

a guest
Dec 14th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. from urllib.request import Request, urlopen
  2. from bs4 import BeautifulSoup
  3. import datetime
  4. import csv
  5.  
  6. req = Request('https://www.google.com/search?q=euro+value')
  7.  
  8. # add user-agent header to our request, a simple string identifying the browser
  9. # in this case the user-agent is for Firefox 24, the current ESR
  10. req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0")
  11.  
  12. # query the website and return the html to the variable ‘currency_page’
  13. currency_page = urlopen(req).read()
  14.  
  15. # parse the html using beautiful soap and store in variable `soup`
  16. soup = BeautifulSoup(currency_page, 'html.parser')
  17.  
  18. # Get 'span' WITH 'title' element:
  19. # https://stackoverflow.com/questions/38133759/how-to-get-text-from-span-tag-in-beautifulsoup
  20. # WITHOUT ' title'
  21. # https://stackoverflow.com/questions/35967854/scraping-with-beautifulsoup-object-has-no-attribute
  22. latest_value = soup.find_all('span')
  23.  
  24. # Create list to access elements by numerical ID
  25. myList = []
  26.  
  27. # Populate list with 'span' elements from webpage
  28. for i in latest_value:
  29. myList.append(i.text)
  30.  
  31. # Access the $ element and save it
  32. now_value = myList[10]
  33.  
  34. # FOR CSV FILE - Date & Time
  35. now = datetime.datetime.now()
  36.  
  37. # Append to CSV
  38. # http://beancoder.com/csv-files-using-python/
  39.  
  40. with open('currency_historical_values.csv','a', newline='') as newFile:
  41. newFileWriter = csv.writer(newFile)
  42. newFileWriter.writerow(now_value)
  43.  
  44. In [47]: %run "C:/Users/[me]/Documents/0_Personal/Coding/Python/CurrencyTrackingProject.py"
Add Comment
Please, Sign In to add comment