0

So I have been trying to create a script where there is a countdown with epoch time which I later gonna convert it.

The html is following:

<script type="text/javascript">
                new Countdown('countdown_timer', '1547161260', 'https://stackoverflow.com/');
            </script>

and I started to scrape it which I managed to scrape using:

try:
    time_countdown_tag = bs4.find_all('script', {'type': 'text/javascript'})
except Exception:
    time_countdown_tag = []

for countdown in time_countdown_tag:
    if 'new Countdown' in countdown.text.strip():
        print(countdown)

which my output is:

<script type="text/javascript">
                new Countdown('countdown_timer', '1547161260', 'https://stackoverflow.com/');
            </script>

However what I want to print out in this case is the number inside the params which is 1547161260 - I would appreciate all kind of help to be able to be able to only print out the number (epoch) if it is possible?

1 Answer 1

1

You can use regular expressions to match the portion of the JS that contains a positive integer:

import re
output = """<script type="text/javascript">
                new Countdown('countdown_timer', '1547161260', 'https://stackoverflow.com/');
            </script>"""
re.findall("\d+", output)
Sign up to request clarification or add additional context in comments.

4 Comments

Hello! Oh yeah! However I do get TypeError: expected string or bytes-like object when I try this code :O could it be becuase of the spaces \t ?
My guess is to do maybe countdown.text.strip() before doing re.findall("\d+", output)
That would work. I'm guessing you're passing in countdown which is an object, and countdown.text (or str(countdown)) would be the string representation.
Exactly, I guess thats what my thoughts were, It did work when I passed it as a text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.