2

I have been trying to parse the table here with table id = "tblDataset2" and trying to access the rows in the table, but I only get a single row when I parse the webpage using beautifulsoup. Here's my code :

from bs4 import BeautifulSoup
import requests

URL = 'http://podaac.jpl.nasa.gov/ws/'
dataset_ids = []
html = requests.get(URL + 'search/granule/index.html')
soup = BeautifulSoup(html.text, 'html.parser')

table = soup.find("table", {"id": "tblDataset2"})
rows = table.find_all('tr')
rows.remove(rows[0])
print table
for row in rows:
    x = row.find_all('td')
    dataset_ids.append(x[0].text.encode('utf-8'))

print dataset_ids

I want to access all the rows of the table. Please help me with this. Thanks.

1 Answers1

1

This particular dataset is being asynchronously loaded by the browser from a different endpoint which returns a JSON response. Make the request directly to that endpoint:

import requests

URL = 'http://podaac.jpl.nasa.gov/l2ssIngest/datasets'
response = requests.get(URL)
data = response.json()

for item in data["datasets"]:
    print(item["persistentId"], item["shortName"])

Prints:

(u'PODAAC-AQR40-2SOCS', u'AQUARIUS_L2_SSS_V4')
(u'PODAAC-QSX12-L2B01', u'QSCAT_LEVEL_2B_OWV_COMP_12')
(u'PODAAC-ASOP2-12C01', u'ASCATA-L2-Coastal')
(u'PODAAC-ASOP2-25X01', u'ASCATA-L2-25km')
(u'PODAAC-ASOP2-25B01', u'ASCATB-L2-25km')
(u'PODAAC-ASOP2-COB01', u'ASCATB-L2-Coastal')
(u'PODAAC-J2ODR-GPS00', u'OSTM_L2_OST_OGDR_GPS')
(u'PODAAC-OSCT2-L2BV2', u'OS2_OSCAT_LEVEL_2B_OWV_COMP_12_V2')
(u'PODAAC-RSX12-L2B11', u'RSCAT_LEVEL_2B_OWV_COMP_12_V1.1')
(u'PODAAC-AKASA-XOGD1', u'ALTIKA_SARAL_L2_OST_XOGDR')
(u'PODAAC-GHAM2-2PR72', u'AMSR2-REMSS-L2P-v7.2')
(u'PODAAC-GHVRS-2PN01', u'VIIRS_NPP-NAVO-L2P-v2.0')
(u'PODAAC-RSX12-L2B12', u'RSCAT_LEVEL_2B_OWV_COMP_12_V1.2')

As for the first dataset, you need to make a GET request to the "search" endpoint:

from operator import itemgetter

import requests


URL = 'http://podaac.jpl.nasa.gov/dmasSolr/solr/dataset/select/'
response = requests.get(URL, params={
    'q': '*:*',
    'fl': 'Dataset-PersistentId,Dataset-ShortName-Full',
    'rows': '2147483647',
    'fq': 'DatasetPolicy-AccessType-Full:(OPEN OR PREVIEW OR SIMULATED OR REMOTE) AND DatasetPolicy-ViewOnline:Y',
    'wt': 'json'
})
data = response.json()

for doc in sorted(data['response']['docs'], key=itemgetter('Dataset-ShortName-Full')):
    print(doc['Dataset-PersistentId'], doc['Dataset-ShortName-Full'])

Prints:

(u'PODAAC-GHRAM-4FA01', u'ABOM-L4HRfnd-AUS-RAMSSA_09km')
(u'PODAAC-GHGAM-4FA01', u'ABOM-L4LRfnd-GLOB-GAMSSA_28km')
(u'PODAAC-AKASA-XOGD1', u'ALTIKA_SARAL_L2_OST_XOGDR')
(u'PODAAC-USWCO-ALT01', u'ALT_TIDE_GAUGE_L4_OST_SLA_US_WEST_COAST')
...
(u'PODAAC-SASSX-L2WAF', u'WAF_DEALIASED_SASS_L2')
(u'PODAAC-SMMRN-2WAF0', u'WENTZ_NIMBUS-7_SMMR_L2')
(u'PODAAC-SASSX-L2SN0', u'WENTZ_SASS_SIGMA0_L2')

If you prefer not to dive into how the page is loaded and formed, you can use a real browser automated by selenium.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Can you also let me know the url to access the datasets of the table id: "tblDataset" of the same page. Thanks. – Omkar Reddy Aug 26 '16 at 14:19
  • @OmkarReddy sure, this one is a little bit more complicated as it would a search request to http://podaac.jpl.nasa.gov/dmasSolr/solr/dataset/select/ endpoint with multiple GET parameters. Let me see if I can provide you with a sample. By the way, just curious, what are you parsing these datasets for? Thanks! – alecxe Aug 26 '16 at 14:22
  • @OmkarReddy okay, updated with a working code for the first dataset. – alecxe Aug 26 '16 at 14:29
  • I have been working on an Apache project, Apache Open Climate Workbench and I am building a data source for it to integrate into it, you can find it here https://github.com/lewismc/podaacpy. Thanks for the help. :) – Omkar Reddy Aug 27 '16 at 03:37