If Conditions Split (String/Float to INT)

Good afternoon fellow automators.

I am facing an interesting problem ( which unfortunately i am unable to find a solution for) and that’s why i am requesting help from the beautiful minds over here.

I am building a web scraper for YouTube ( no api or anything because #costs).
Currently my robot goes on YouTube, searches by some keywords and filters by upload date and then takes the number of views of the videos on the page. Then it trims the string and transforms it into an INT.

The problem is that the views on YouTube after 999 go to 1K/1.1K/1.2K and so on. I have already fixed an exception where some videos have no views and i iffed that out. Long story short what i would need guidance on is, i need to transform/multiply that string and remove the K at the end and then transform the variable float (1.2) in 1200 in order to convert it to string.

As you can see in the if where i wrote the OR i have no idea how to transform it.

Please let me know if it makes sense what i tried to explain.

** i have no idea how to attach screenshots while creating the topic
** found it


image

@Daniel-F,

To remove K use as below
views_number = str.Remove(views_number.IndexOf(“K”))

To convert String to int use as below,
int_views_number = CStr(views_number)

please let us know if you’re stuck…

Cheers,
Pankaj

Hi,

Can you try the following?

Double.Parse(System.Text.RegularExpressions.Regex.Match(yourString,"^[.\d]+").Value)*Math.Pow(10,3*Array.IndexOf(arr,System.Text.RegularExpressions.Regex.Match(yourString,"[A-Z]$").Value))

Sequence1.xaml (5.5 KB)

Regards,

2 Likes

Thank you for the quick answer Yoichi, it works like a charm (on a test file).

But i do have a question though and forgive me if it sounds stupid but, given that i have one single variable for the number of views ( views_number ) which pulls from 1 view to 1K /1M/1B and i have the exception if ( if the string = “No views” then do nothing ) , how can i integrate that into my IF?

Also, is there any reason why i am unable to assign the Double Parse?

Hi,

how can i integrate that into my IF?

Can you try the following condition in If activity?

System.Text.RegularExpressions.Regex.IsMatch(yourString,"^[.\d]+[KMB]?$")

img20210419-8

Note : If there are some extra space at the beginning /end of the string. you need to trim it using Trim method in advance.

Also, is there any reason why i am unable to assign the Double Parse?

Is int_views_number is String type variable? If so, you need to add parentheses and .ToString at the end of the expression as the following.

(Double.Parse(System.Text.RegularExpressions.Regex.Match(yourString,"^[.\d]+").Value)*Math.Pow(10,3*Array.IndexOf(arr,System.Text.RegularExpressions.Regex.Match(yourString,"[A-Z]$").Value))).ToString

Regards,

1 Like

Got it, so what i did was this :


image

And it seems to be working just fine. thank so so very much! i will mark you answer as a solution, but can i post more questions here if i have later on ?

1 Like

Hi,

Yes, but if your additional question is far from the title, perhaps you should create another topic, i think. (And someone (include me) will reply it.)

Regards,

1 Like

Hi,
Do you try to get the tooltip attribute. There you have the exact number of views.

image

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.