I've been working on a webscraper which runs asynchronously to substantially improve speed. Because of the way scraper works, it writes two pieces of information for a url which it scrapes. These are stored in two dictionaries:
# {url1: [parent1, parent2, parent3], url2: [parent1, parent2, parent3]...}
relationships = dict()
# {url1: [page, redirect, content_type...], url2: [page, redirect, content_type...]}
url_results = dict()
The first dictionary, relationships, tracks each occurrence of the key url within a parent url The second dictionary stores the resulting information I need from each url scraped.
This is all necessary for how I'm handling async threading and memorization, and works really nice. The problem lies in writing these results to an excel report. These dictionaries are getting to thousands of keys in size and merging them together is very slow.
The end result is that for each parent value for a url in relationships dict, a line is written in my excel sheet
| parent | url | page | redirect | content type | etc |
|---|---|---|---|---|---|
| parent1 | url1 | page | redirect | content_type | etc... |
| parent1 | url2 | page | redirect | content_type | etc... |
| parent1 | url3 | page | redirect | content_type | etc... |
| parent2 | url1 | page | redirect | content_type | etc... |
| parent2 | url2 | page | redirect | content_type | etc... |
| parent2 | url3 | page | redirect | content_type | etc... |
Here's the method I'm currently using to merge these dictionaries together like this:
partent_result_df = pd.DataFrame(columns=["Parent", "URL", "Page", "Redirect", "Content-type", "Source", "Status"])
location = 1
for i, key in enumerate(RELATIONSHIPS):
# result_arr = [Item, Redirect, Content_type, Source, Status]
try:
result_arr = URL_RESULTS[key]
except:
result_arr = [None, None, None, None, "Not Yet Accessed"]
parents_urls = RELATIONSHIPS[key]
for parent in parents_urls:
partent_result_df.loc[location] = [parent, key, *result_arr]
location += 1
return partent_result_df
I'm looking for a way to make this merge much faster if at all possible. It's currently taking quite a lot of time to write this report, almost as much as the scrape itself, and I think I'm missing something that could help speed up this process.
I'd appreciate any help finding a better way to do this. Sorry for the information overkill, and thank you greatly for any help!