Skip to main content

Complex Web Scraping with Error Handling

Loop through URLs, scrape each with TryCatch error handling, and conditionally save results

Steps

#ActivityWhat it does
1StartWebBrowserOpen browser for scraping session
2CreateTableCreate an in-memory table to accumulate scraped rows
3ForEachLoopIterate over list of URLs to scrape
4TryCatchHandle errors for each URL gracefully
5IfConditionCheck if data was extracted before saving
6TableUpsertRowAppend a scraped row to the accumulated table
7TableExportExport the accumulated rows to Excel

Workflow

urls = Assign({'urls': ['https://example.com/page1', 'https://example.com/page2', 'https://example.com/page3']})
StartWebBrowser("https://example.com", "useCurrentBrowserSession")
CreateTable("allResults", schema={"columns": [{"name": "url"}, {"name": "content"}]})
for currentUrl in urls:
try:
NewChangeUrlBrowserTab(currentUrl)
pageData = GetHtmlElements("//div[@class='content']")
if pageData.length > 0:
for item in pageData:
TableUpsertRow("allResults", {"url": currentUrl, "content": "{{ item.text }}"}, append=True)
else:
WriteLine("{{ 'No data found at index ' + urlIndex }}")
except Exception as scrapeError:
WriteLine("{{ 'Error scraping URL at index ' + urlIndex + ': ' + scrapeError }}")
TableExport("allResults", "./scraped_results.xlsx", format="xlsx", sheetName="Results")