Further table url link scraping

Viewed 41

Using this awesome code I can scrape the majority of the data, however it seems like some of the url's may not scrape. Also I need to be able to identify the data source in the future. Therefore I have two question:

  • how to program in a check that all links are identified and scraped?

  • how to insert the url link as a new column so each row is identified?

      url = 'https://www.rootsandrain.com/organiser21/uci/events/filters/dh/'
      response = requests.get(url)
      soup = BeautifulSoup(response.content)
    
      urlList = ['https://www.rootsandrain.com'+row.a['href'] for row in soup.select('#T1 tbody tr')]
    
      data = []
    
      for url in urlList:
          try:
              data.append(pd.read_html(url)[0])
          except:
              print(f'No tables found:{url}')
    
      pd.concat(data)
    

Original post

Scraping URL links in a table

1 Answers

A small adjustment to the try/except block, that writes the url and also the status of scraping:

    try:
        df = pd.read_html(url)[0]
        df['url'] = url
        df['scraped'] = True
        data.extend(df.to_dict(orient = 'records'))
    except:
        print(f'No tables found:{url}')
        data.append(dict(url=url, scraped=False))

Example

Limited to for url in urlList[5:10] to not iterate all of the links in this example, just kick the [5:10] to get all:

import requests
from bs4 import BeautifulSoup
import pandas as pd

url = 'https://www.rootsandrain.com/organiser21/uci/events/filters/dh/'
response = requests.get(url)
soup = BeautifulSoup(response.content)

urlList = ['https://www.rootsandrain.com'+row.a['href'] for row in soup.select('#T1 tbody tr')]

data = []

for url in urlList[5:10]:
    try:
        df = pd.read_html(url)[0]
        df['url'] = url
        df['scraped'] = True
        data.extend(df.to_dict(orient = 'records'))
    except:
        print(f'No tables found:{url}')
        data.append(dict(url=url, scraped=False))
        
pd.DataFrame(data)

#to get rid of all these NaN columns
#pd.DataFrame(data).dropna(axis=1, how='all')

Output

url scraped Pos⇧ Bib Name Licence YoB Sponsors km/h sector1 + sector2 + sector3 + sector4 + sector5 = Qualifier km/h.1 sector1 +.1 sector2 +.1 sector3 +.1 sector4 +.1 sector5 =.1 Run 1 Diff
https://www.rootsandrain.com/event9599/2022-jul-9-mercedes-benz-uci-world-cup-dh-4-lenzerheide/ False nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
https://www.rootsandrain.com/event9598/2022-jun-11-mercedes-benz-uci-world-cup-dh-3-leogang/ False nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
https://www.rootsandrain.com/event9597/2022-may-22-mercedes-benz-uci-world-cup-dh-2-fort-william/ False nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
https://www.rootsandrain.com/event9607/2022-apr-23-dhi-masters-world-championships-22-villa-la-angostura/ False nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 1st 6 Amaury PIERRON 1.00088e+10 1996 COMMENCAL - MUC-OFF BY RIDING 62.211 45.680s5 32.179s3 36.880s2 27.636s5 28.399s8 2:50.7743 60.3411 44.269s1 33.091s23 36.159s1 26.774s2 27.418s1 2:47.7111 -
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 2nd 14 Finn ILES 1.00909e+10 1999 Specialized Gravity 60.072 44.915s3 32.242s5 37.407s5 27.636s5 28.254s3 2:50.4542 59.0821 44.924s2 31.474s3 37.175s4 26.781s3 28.204s6 2:48.5582 0.847s
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 3rd 1 Loïc BRUNI 1.00075e+10 1994 Specialized Gravity 59.6027 45.080s4 31.429s1 37.254s4 27.164s2 28.344s6 2:49.2711 58.8225 45.023s4 31.653s6 37.112s3 27.172s5 27.837s3 2:48.7973 1.086s
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 4th 8 Benoit COULANGES 1.00082e+10 1994 Dorval AM Commencal 60.671 46.785s15 31.773s2 36.688s1 27.496s3 28.348s7 2:51.0905 59.0821 45.302s7 31.591s5 37.745s9 26.711s1 27.828s2 2:49.1774 1.466s
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 5th 17 Luca SHAW 1.00088e+10 1996 Canyon Collective Factory Team 59.0836 46.267s7 32.952s14 38.067s10 28.504s17 28.453s9 2:54.24310 61.223 45.223s6 31.572s4 37.436s7 27.629s12 28.282s10 2:50.1425 2.431s
https://www.rootsandrain.com/event9596/2022-mar-27-mercedes-benz-uci-world-cup-dh-1-lourdes/ True 6th 9 Danny HART (elt) 1.00055e+10 1991 CUBE FACTORY RACING 59.8722 47.041s22 32.715s8 37.246s3 28.000s11 28.301s4 2:53.3037 60.0714 46.511s21 31.868s7 36.600s2 27.391s10 28.036s4 2:50.4066 2.695s
Related