Skip to content
Advertisement

get a list from html source using python3

I am trying to get Cases list of COVID-19 positive cases from https://www.worldometers.info/, e.g. this

The sample looks like(~line no: 700) :

<script type="text/javascript">
    Highcharts.chart('coronavirus-cases-linear', {
        chart: {
            type: 'line'
        },
        title: {
            text: 'Total Cases'
        },

        subtitle: {
            text: '(Linear Scale)'
        },

        xAxis: {
            categories: ["Feb 15","Feb 16","Feb 17","Feb 18","Feb 19","Feb 20","Feb 21","Feb 22","Feb 23","Feb 24","Feb 25","Feb 26","Feb 27","Feb 28","Feb 29","Mar 01","Mar 02","Mar 03","Mar 04","Mar 05","Mar 06","Mar 07","Mar 08","Mar 09","Mar 10","Mar 11","Mar 12","Mar 13","Mar 14","Mar 15","Mar 16","Mar 17","Mar 18","Mar 19","Mar 20","Mar 21","Mar 22","Mar 23","Mar 24","Mar 25","Mar 26","Mar 27","Mar 28","Mar 29","Mar 30","Mar 31","Apr 01","Apr 02","Apr 03","Apr 04","Apr 05","Apr 06","Apr 07","Apr 08","Apr 09","Apr 10","Apr 11"]        },

        yAxis: {
            title: {
                text: 'Total Coronavirus Cases'
            }


        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },

        credits: {
            enabled: false
        },


        series: [{
            name: 'Cases',
            color: '#33CCFF',
            lineWidth: 5,
            ## I NEED THIS LIST
            data:   [2,2,2,2,2,2,2,2,2,3,9,13,25,33,58,84,120,165,228,282,401,525,674,1231,1695,2277,3146,5232,6391,7988,9942,11826,14769,18077,21571,25496,28768,35136,42058,49515,57786,65719,73235,80110,87956,95923,104118,112065,119199,126168,131646,136675,141942,148220,153222,158273,163027]        }],
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 800
                },
                chartOptions: {
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        }

    });

I am using bs4 as:

#!/usr/bin/env python3
import requests as req
from bs4 import BeautifulSoup as bs

resp = req.get("https://www.worldometers.info/coronavirus/country/spain/")
soup = bs(resp.text, 'lxml')
scripts = soup.find_all("script")
for script in scripts:
  if "Cases" in script.series:
    print(script.name)

which does scrape the file, but after that I am clueless how to get the data. The list I am looking for is commented with ## I NEED THIS LIST. Kindly help.

Advertisement

Answer

You could write a regex for it

import requests as req
import re

resp = req.get("https://www.worldometers.info/coronavirus/country/spain/")
p = re.compile(r"(?<=name:s'Cases')[sS]+?data:s([.*?])")
p.findall(resp.text)[0]

enter image description here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement