-1

I have a problem i first thought many would experience, but i couldn't find any similar questions. The language i'm using is Python.

I want to read a specific value from a website, which is embedded in another code behind. I first thought this approach here could work. (Downloading the html page, then reading a specific line). But the problem is, that the value i am looking for is generated constantly in another class or code. So basically when i tried to look at the html-code with Chrome, i couldn't find my preferred value.

The page i am trying to read: Page. The value i need is the Price per Ether in Euro.

I appreciate your help!

Community
  • 1
  • 1
Chris
  • 1
  • Welcome to SO, please be a bit more specific when asking question: what have you tried, what do you expect, etc. See [how to ask](http://stackoverflow.com/help/how-to-ask) – Nehal Jul 27 '16 at 12:52
  • If the value was generated by javascript you will need something like selenium to extract it – joel goldstick Jul 27 '16 at 12:53
  • I basically downloaded the html of the page and searched for the value. But the value is created by a javascript-function. So how can i access the value i need with code in python? – Chris Jul 27 '16 at 12:54
  • I should add that i just started learning to code, so have mercy with me :) – Chris Jul 27 '16 at 12:55
  • I just confirmed that the page is created with javascript. So google selenium and you will find some examples how to proceed. – joel goldstick Jul 27 '16 at 12:56
  • @joelgoldstick Thank you. I'll have a look at it! – Chris Jul 27 '16 at 12:57
  • also here: http://stackoverflow.com/questions/36237290/web-scraping-with-selenium – joel goldstick Jul 27 '16 at 12:58
  • thank you again :) – Chris Jul 27 '16 at 13:00
  • @joelgoldstick So i tried navigating to the value with selenium. I used the chrome webdriver to see what the program is really doing. I've come this far: driver = webdriver.Chrome(chrome_path) driver.get("http://ether.price.exchange/") driver.find_element_by_xpath("""//*[@id="currency"]""").click() driver.find_element_by_xpath("""//*[@id="EUR"]""").click() The problem now is that i can't select the value and get it. When inspecting with chrome it says in the line ... value='loading...'. – Chris Jul 27 '16 at 13:46
  • Do you have an idea how i could get the value? – Chris Jul 27 '16 at 13:49
  • i tried using: driver.find_element_by_xpath("""//*[@id="EUR"]""").get_attribute("value") but i only get the output "none" – Chris Jul 27 '16 at 13:53

2 Answers2

1

The data on the page comes from an XHR loaded json blob, which it is possible to query directly.

>>> import requests
>>> import pprint
>>> r = requests.get('http://ether.price.exchange/update')
>>> pprint.pprint(r.json())
{u'AUD': {u'15m': 873.83,
          u'buy': 873.83,
          u'last': 873.83,
          u'sell': 873.85,
          u'symbol': u'$'},
 u'BRL': {u'15m': 2140.39,
          u'buy': 2140.39,
          u'last': 2140.39,
          u'sell': 2140.42,
          u'symbol': u'R$'},
 u'CAD': {u'15m': 860,
          u'buy': 860,
          u'last': 860,
          u'sell': 860.02,
          u'symbol': u'$'},
 u'CHF': {u'15m': 643.67,
          u'buy': 643.67,
          u'last': 643.67,
          u'sell': 643.68,
          u'symbol': u'CHF'},
 u'CLP': {u'15m': 428297.17,
          u'buy': 428297.17,
          u'last': 428297.17,
          u'sell': 428303.73,
          u'symbol': u'$'},
 u'CNY': {u'15m': 4359.5,
          u'buy': 4359.5,
          u'last': 4359.5,
          u'sell': 4359.56,
          u'symbol': u'\xa5'},
 u'DKK': {u'15m': 4416.7,
          u'buy': 4416.7,
          u'last': 4416.7,
          u'sell': 4416.76,
          u'symbol': u'kr'},
 u'EUR': {u'15m': 593.66,
          u'buy': 593.66,
          u'last': 593.66,
          u'sell': 593.67,
          u'symbol': u'\u20ac'},
 u'GBP': {u'15m': 496.02,
          u'buy': 496.02,
          u'last': 496.02,
          u'sell': 496.02,
          u'symbol': u'\xa3'},
 u'HKD': {u'15m': 5062.79,
          u'buy': 5062.79,
          u'last': 5062.79,
          u'sell': 5062.87,
          u'symbol': u'$'},
 u'ISK': {u'15m': 79579.79,
          u'buy': 79579.79,
          u'last': 79579.79,
          u'sell': 79581.01,
          u'symbol': u'kr'},
 u'JPY': {u'15m': 69110.23,
          u'buy': 69110.23,
          u'last': 69110.23,
          u'sell': 69111.28,
          u'symbol': u'\xa5'},
 u'KRW': {u'15m': 742032.87,
          u'buy': 742032.87,
          u'last': 742032.87,
          u'sell': 742044.24,
          u'symbol': u'\u20a9'},
 u'NZD': {u'15m': 933.8,
          u'buy': 933.8,
          u'last': 933.8,
          u'sell': 933.82,
          u'symbol': u'$'},
 u'PLN': {u'15m': 2589.46,
          u'buy': 2589.46,
          u'last': 2589.46,
          u'sell': 2589.5,
          u'symbol': u'z\u0142'},
 u'RUB': {u'15m': 42472.95,
          u'buy': 42472.95,
          u'last': 42472.95,
          u'sell': 42473.6,
          u'symbol': u'RUB'},
 u'SEK': {u'15m': 5637.68,
          u'buy': 5637.68,
          u'last': 5637.68,
          u'sell': 5637.77,
          u'symbol': u'kr'},
 u'SGD': {u'15m': 887.79,
          u'buy': 887.79,
          u'last': 887.79,
          u'sell': 887.81,
          u'symbol': u'$'},
 u'THB': {u'15m': 22835.96,
          u'buy': 22835.96,
          u'last': 22835.96,
          u'sell': 22836.31,
          u'symbol': u'\u0e3f'},
 u'TWD': {u'15m': 20965.35,
          u'buy': 20965.35,
          u'last': 20965.35,
          u'sell': 20965.67,
          u'symbol': u'NT$'},
 u'USD': {u'15m': 652.7,
          u'buy': 652.7,
          u'last': 652.7,
          u'sell': 652.71,
          u'symbol': u'$'},
 u'baseVolume': u'71691.55099130',
 u'high': u'0.02070000',
 u'high24hr': u'0.02070000',
 u'highestBid': u'0.01957006',
 u'id': 148,
 u'isFrozen': u'0',
 u'last': u'0.01956700',
 u'low': u'0.01760000',
 u'low24hr': u'0.01760000',
 u'lowestAsk': u'0.01958372',
 u'percentChange': u'0.07570270',
 u'price': u'0.01956700',
 u'quoteVolume': u'3802775.62565674',
 u'volume': u'71691.55099130'}

Reading the javascript in the page, the price of 1 ether in a currency is 1 * data['price'] * data['EUR']['last']:

>>> r = requests.get('http://ether.price.exchange/update')
>>> d = r.json()
>>> float(d['price']) * float(d['EUR']['last'])
11.562597087999999
MattH
  • 37,273
  • 11
  • 82
  • 84
  • Worth noting that at time of writing, there appears to be a bug on the website that the per currency value updated periodically is always the USD calculation even after you've selected a different currency from the dropdown. – MattH Jul 27 '16 at 13:40
  • Thank's anyway! Where did you see that the data is coming from a json blob? – Chris Jul 27 '16 at 13:50
  • 1
    I guessed that that would be the case and looked for XHR in chrome dev tools. – MattH Jul 27 '16 at 13:54
0

I was able to get the value from another webpage. The code looks like this:

def get_current_value(): chrome_path = r"C:\Users\Chris\Desktop\Chrome_driver\chromedriver.exe" driver = webdriver.Chrome(chrome_path) driver.get("https://cryptowatch.de/kraken/etheur") a = driver.find_element_by_xpath("""//*[@id="price-ticker"]""").text unicodedata.normalize("NFD",a)#.encode('ascii','ignore') return a

I added this code here unicodedata.normalize("NFD",a)#.encode('ascii','ignore') to transform the output, which was apparently unicode, to a string.

The problem i face now, is that the output for a is something like : €12.99
How can i remove the euro sign so i can transform the string to a float?

I have to post this as answer since someone downvoted me for no reason so i can't ask another question today..

Chris
  • 1
  • I got it finally! Since the euro sign is always the character 0 i can use the following code: a = float(a[1:]). Didn't think it could be so easy :) – Chris Jul 27 '16 at 16:19