Create google search parser #349
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Google Search Parser - Challenge Solution
Solution
Created a
GoogleSearchParser
service with specs to validate the expected behavior.The
GoogleSearchParser
gathers image information as long as the basic image structure exists on the page, regardless of the rest of the HTML structure. You can see this demonstrated in the test case: when HTML has malformed structure but with valid data.Supported Image Structures
Minimal structure:
Maximum structure:
Image Source Handling
I noticed that Google populates the carousel image sources dynamically using JavaScript. To address this, I implemented a function
extract_image_base64_by_id()
that extracts the source directly from the script tags.Name Extraction Logic
For artwork names, the parser follows a fallback strategy:
div
with classpgNMRc
alt
attributeThis approach ensures robust name extraction even when the HTML structure varies or is incomplete.
Technical Implementation
pablo_picasso
,claude_monet
, andleonardo_da_vinci
for cross-validationSetup
Usage
Initialize GoogleSearchParser with the HTML. The parse_images method returns the JSON result.
Testing
The solution includes tests for various scenarios including malformed HTML structures to ensure robust parsing capabilities.