0

Yesterday I wrote a script which scraped a value from a webpage, and then removed two special characters ('.' and '€') from the scraped string, so it could then be converted to integer format. Today, nonetheless, the code isn't replacing the '€' sign.

The value I get from the webpage is '5.000 €', and to replace the two characters I use the code:

amt = driver.find_element_by_class_name('example-val').text
amt = int(amt.replace('.', '').replace('€', ''))

I have already tried using re.sub, without results:

amt = re.sub('.|€', '', amt)

I don't know whether this is the best approach to convert the string to integer format, but it's what I came up with.

Any help would be greatly appreaciated!

stark
  • 12,615
  • 3
  • 33
  • 50
martifapa
  • 107
  • 3
  • 12
  • Are you using Python2? On Python3 this works. – user69453 Apr 05 '20 at 11:13
  • What puzzles me is that I'm using Python3, and it doesn't work (though yesterday did)! – martifapa Apr 05 '20 at 11:45
  • We have no way of knowing the value of ˋdriver.find_element_by_class_name('example-val').textˋ. Can you please provide some sample value so that your code is a minimal, reproducible example? Are you sure that your conversion code stopped working and not, say, your scraping code returns something else because the website changed? – MisterMiyagi Apr 05 '20 at 11:55
  • 5.000 € <-- This is what I get from copy-pasting from the web directly. Also, for debugging puproses, I tried printing the scraped string before trying to apply any conversions (just in case the value had changed), and the value hadn't changed. As I told you, it's strange because yesterday the code worked just fine, but today it doesn't (though I didn't apply any changes!). – martifapa Apr 05 '20 at 12:03
  • I cannot reproduce your problem given this input. Just assigning this value to amt instead of getting it from the driver runs the conversion properly. – MisterMiyagi Apr 05 '20 at 12:08
  • I see... Maybe I could tell you the page from which I'm getting the data (and how I'm getting it, if needed)? Of course, if you don't want to take the trouble you don't have to! The solution below worked for me, though it would be nice to know what was wrong with my original code. – martifapa Apr 05 '20 at 12:17

1 Answers1

0

In a more simpler way try this:

amt = driver.find_element_by_class_name('example-val').text
amount=''
for i in amt:
    if i in '0123456789':
        amount+=i
amount=int(amount)

As a One-Liner:

amt = driver.find_element_by_class_name('example-val').text
#Either:
amt = int( ''.join(filter(lambda i: i.isdigit(), amt)))
#or:
amt = int(re.sub("\D", "", amt))
Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34