0

I need to read a list of webpages. For instance I have a web that works

url1 = 'https://www.sia.ch/en/membership/member-directory/m/207778/'
driver = webdriver.Chrome('/Users/massaro/research/Valeria/chromedriver 2')
driver.get(url1)
html = driver.page_source
html = str(html).replace('<br />', '::')
df1 = pd.read_html(html)[0].iloc[[0,2],1]

than I have another page for which I get the error

url2 = 'https://www.sia.ch/en/membership/member-directory/m/105531/'
driver = webdriver.Chrome('/Users/massaro/research/Valeria/chromedriver 2')
driver.get(url2)
html = driver.page_source
html = str(html).replace('<br />', '::')
df1 = pd.read_html(html)[0].iloc[[0,2],1]

ValueError: No tables found

I would like to have a condition to skip webpages that have no tables in order to avoid the error.

emax
  • 6,965
  • 19
  • 74
  • 141
  • But you have such a condition. Pages without tables will raise `ValueError`. That is a testable condition. – Jongware Feb 13 '20 at 19:11
  • @usr2564301 yes but how can I check it before to get the error? – emax Feb 13 '20 at 19:13
  • "Before", no you don't. Catching errors is a [standard procedure in Python](https://docs.python.org/3/tutorial/errors.html#handling-exceptions). – Jongware Feb 13 '20 at 19:16

1 Answers1

0

Maybe this will help? You could check if the value returned by this is truthy/falsy:

if driver.find_element_by_css_selector("table"):
   # rest of your code here

Something along those lines?

Roah
  • 1
  • 3