Python Forum
Code is Working As-Is, Looking for Pointers to Improve
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code is Working As-Is, Looking for Pointers to Improve
#5
Here's a good way to get your data.
I didn't finish it all, but show enough for you to understand how it works.
you will need:
  1. All installs from command line.
  2. requests, install with: pip3 install requests
  3. BeautifulSoup, install with: pip3 install BeautifulSoup4
  4. lxml, install with: pip3 install lxml

There are some printouts (calculatedconditions and calculatedvhfconditions) that are only there to show how this
data is loaded into a list. You can remove the print statements.
Note that I used one of the items from calculatedvhfconditions in the results (VHF) this shows how to fetch from list

You will have to finish display of remaining data. If you have any questions, please ask.
'''
    outputs:

    Rubric 1
        SFI: 120 / K: 2 / A: 4 / Sunspots: 85 / SNR: S1-S2 / MUF: 20.54 / VHF: Band Closed

    Rubric 2
        80-40:Fair 30-20:Good 17-15:Fair 12-10:Fair 30-20:Good 17-15:Fair 12-10:Poor

'''
import requests
# from PrettifyPage import PrettifyPage
from bs4 import BeautifulSoup
from TryPaths import TryPaths
from CreateDict import CreateDict
from xml_to_dict import XMLtoDict


class GetSolarData:
    def __init__(self):
        self.tpath = TryPaths()
        # self.pp = PrettifyPage().prettify
        self.cd = CreateDict()
        self.parser = XMLtoDict()
        self.data = {}

    def dispatch(self):
        data = self.get_data()
        self.process_data(data)

    def get_data(self):
        url = 'http://www.hamqsl.com/solarxml.php'
        response = requests.get(url)
        if response.status_code == 200:
            return BeautifulSoup(response.content, 'lxml')
        else:
            print(f"problem retreving data, status: {response.status_code}")
            return None

    def process_data(seld, data):
        solardata = data.find('solardata')
        calculatedconditions = solardata.calculatedconditions.find_all('band')
        # This print statement just to show list contents -- remove when all working
        print(f"\ncalculatedconditions: {calculatedconditions}")

        calculatedvhfconditions = solardata.calculatedvhfconditions.find_all('phenomenon')
        # This print statement just to show list contents -- remove when all working
        print(f"\ncalculatedvhfconditions: {calculatedvhfconditions}")

        # This is results print --- add similar for remaining data
        print(f"\nSFI: {solardata.solarindex} / K: {solardata.kindex.text} / " \
            f"A: {solardata.aindex.text} / Sunspots: {solardata.sunspots.text} / " \
            f"SNR: {solardata.snr} / MUF: {solardata.muf.text} / VHF: {calculatedvhfconditions[2].text}")


def main():
    gsd = GetSolarData()
    gsd.dispatch()


if __name__ == '__main__':
    main()
results as is:
Output:
calculatedconditions: [<band name="80m-40m" time="day">Fair</band>, <band name="30m-20m" time="day">Good</band>, <band name="17m-15m" time="day">Fair</band>, <band name="12m-10m" time="day">Poor</band>, <band name="80m-40m" time="night">Good</band>, <band name="30m-20m" time="night">Good</band>, <band name="17m-15m" time="night">Fair</band>, <band name="12m-10m" time="night">Poor</band>] calculatedvhfconditions: [<phenomenon location="northern_hemi" name="vhf-aurora">Band Closed</phenomenon>, <phenomenon location="europe" name="E-Skip">High MUF</phenomenon>, <phenomenon location="north_america" name="E-Skip">Band Closed</phenomenon>, <phenomenon location="europe_6m" name="E-Skip">50MHz ES</phenomenon>, <phenomenon location="europe_4m" name="E-Skip">Band Closed</phenomenon>] SFI: None / K: 1 / A: 5 / Sunspots: 64 / SNR: None / MUF: 12.47 / VHF: Band Closed
this is what 'data' looks like (I used python tags to take advantage of scrolling:
<?xml version="1.0" encoding="UTF-8" ?>
<html>
  <body>
    <solar>
      <solardata>
        <source url="http://www.hamqsl.com/solar.html">
          N0NBH
        </source>
        <updated>
          07 May 2022 0934 GMT
        </updated>
        <solarflux>
          119
        </solarflux>
        <aindex>
          5
        </aindex>
        <kindex>
          1
        </kindex>
        <kindexnt>
          No Report
        </kindexnt>
        <xray>
          B6.9
        </xray>
        <sunspots>
          64
        </sunspots>
        <heliumline>
          127.0
        </heliumline>
        <protonflux>
          29
        </protonflux>
        <electonflux>
          1280
        </electonflux>
        <aurora>
          4
        </aurora>
        <normalization>
          1.99
        </normalization>
        <latdegree>
          63.9
        </latdegree>
        <solarwind>
          290.2
        </solarwind>
        <magneticfield>
          -2.2
        </magneticfield>
        <calculatedconditions>
          <band name="80m-40m" time="day">
            Fair
          </band>
          <band name="30m-20m" time="day">
            Good
          </band>
          <band name="17m-15m" time="day">
            Fair
          </band>
          <band name="12m-10m" time="day">
            Poor
          </band>
          <band name="80m-40m" time="night">
            Good
          </band>
          <band name="30m-20m" time="night">
            Good
          </band>
          <band name="17m-15m" time="night">
            Fair
          </band>
          <band name="12m-10m" time="night">
            Poor
          </band>
        </calculatedconditions>
        <calculatedvhfconditions>
          <phenomenon location="northern_hemi" name="vhf-aurora">
            Band Closed
          </phenomenon>
          <phenomenon location="europe" name="E-Skip">
            Band Closed
          </phenomenon>
          <phenomenon location="north_america" name="E-Skip">
            Band Closed
          </phenomenon>
          <phenomenon location="europe_6m" name="E-Skip">
            50MHz ES
          </phenomenon>
          <phenomenon location="europe_4m" name="E-Skip">
            Band Closed
          </phenomenon>
        </calculatedvhfconditions>
        <geomagfield>
          VR QUIET
        </geomagfield>
        <signalnoise>
          S0-S1
        </signalnoise>
        <fof2>
          4.90
        </fof2>
        <muffactor>
          2.04
        </muffactor>
        <muf>
          10.01
        </muf>
      </solardata>
    </solar>
  </body>
</html>
Edit 7:34 EST:
Here's code without extra prints:
'''
    outputs:

    Rubric 1
        SFI: 120 / K: 2 / A: 4 / Sunspots: 85 / SNR: S1-S2 / MUF: 20.54 / VHF: Band Closed

    Rubric 2
        80-40:Fair 30-20:Good 17-15:Fair 12-10:Fair 30-20:Good 17-15:Fair 12-10:Poor

'''
import requests
# from PrettifyPage import PrettifyPage
from bs4 import BeautifulSoup
from TryPaths import TryPaths
from CreateDict import CreateDict
from xml_to_dict import XMLtoDict


class GetSolarData:
    def __init__(self):
        self.tpath = TryPaths()
        # self.pp = PrettifyPage().prettify
        self.cd = CreateDict()
        self.parser = XMLtoDict()
        self.data = {}

    def dispatch(self):
        data = self.get_data()
        self.process_data(data)

    def get_data(self):
        url = 'http://www.hamqsl.com/solarxml.php'
        response = requests.get(url)
        if response.status_code == 200:
            return BeautifulSoup(response.content, 'lxml')
        else:
            print(f"problem retreving data, status: {response.status_code}")
            return None

    def process_data(seld, data):
        solardata = data.find('solardata')
        calculatedconditions = solardata.calculatedconditions.find_all('band')
        calculatedvhfconditions = solardata.calculatedvhfconditions.find_all('phenomenon')

        # This is results print --- add similar for remaining data
        print(f"\nSFI: {solardata.solarindex} / K: {solardata.kindex.text} / " \
            f"A: {solardata.aindex.text} / Sunspots: {solardata.sunspots.text} / " \
            f"SNR: {solardata.snr} / MUF: {solardata.muf.text} / VHF: {calculatedvhfconditions[2].text}")


def main():
    gsd = GetSolarData()
    gsd.dispatch()


if __name__ == '__main__':
    main()
Output:
SFI: None / K: 1 / A: 5 / Sunspots: 64 / SNR: None / MUF: 12.43 / VHF: Band Closed
Reply


Messages In This Thread
RE: Code is Working As-Is, Looking for Pointers to Improve - by Larz60+ - May-07-2022, 11:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to improve this Python 3 code? iamaghost 6 3,550 Jan-18-2021, 06:39 PM
Last Post: iamaghost

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020