Hellosiroverthere
Hellosiroverthere

Reputation: 315

Python - Scrape Javascript using bs4 and print out the value

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?

Upvotes: 0

Views: 144

Answers (1)

Yunyu L.
Yunyu L.

Reputation: 755

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)

Upvotes: 1

Related Questions