-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: extract microdata #3233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: extract microdata #3233
Changes from 9 commits
5aab759
98f94d4
583f8cc
b49e34d
ab4c3be
76ca488
f10398d
bbfb662
9eb5cb8
9dfbee2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import type { Dictionary } from '@crawlee/types'; | ||
| import type { CheerioAPI } from 'cheerio'; | ||
| import { load } from 'cheerio'; | ||
|
|
||
| /** | ||
| * Extract schema.org microdata from an HTML document using Cheerio. | ||
| * | ||
| * @param $ A `CheerioAPI` instance OR raw HTML string. | ||
| * @returns Extracted metadata as a Dictionary. | ||
| */ | ||
| export function extractMicrodata(raw: string): Dictionary<any>; | ||
| export function extractMicrodata($: CheerioAPI): Dictionary<any>; | ||
| export function extractMicrodata(_item: CheerioAPI | string): Dictionary<any> { | ||
| const $ = typeof _item === 'string' ? load(_item) : _item; | ||
|
|
||
| const extractValue = (elem: any) => { | ||
| return $(elem).attr('content') || $(elem).text()?.trim() || $(elem).attr('src') || $(elem).attr('href') || null; | ||
| }; | ||
|
|
||
| const addProperty = (obj: any, propName: string, value: any) => { | ||
| if (typeof value === 'string') value = value.trim(); | ||
|
|
||
| if (Array.isArray(obj[propName])) { | ||
| obj[propName].push(value); | ||
| } else if (obj[propName] !== undefined) { | ||
| obj[propName] = [obj[propName], value]; | ||
| } else { | ||
| obj[propName] = value; | ||
| } | ||
| }; | ||
|
|
||
| const extractItem = (elem: any): any => { | ||
| const item: any = { _type: $(elem).attr('itemtype') }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the spec, |
||
| let count = 0; | ||
|
|
||
| $(elem) | ||
| .find('[itemprop]') | ||
| .filter(function () { | ||
| return $(this).parentsUntil(elem, '[itemscope]').length === 0; | ||
| }) | ||
| .each(function () { | ||
| const propName = $(this).attr('itemprop'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| const value = $(this).is('[itemscope]') ? extractItem(this) : extractValue(this); | ||
|
|
||
| addProperty(item, propName as string, value); | ||
| count++; | ||
| }); | ||
|
|
||
| if (count === 0) { | ||
| addProperty(item, '_value', extractValue(elem)); | ||
| } | ||
|
|
||
| return item; | ||
| }; | ||
|
|
||
| const extractAllItems = () => { | ||
| const items: any[] = []; | ||
|
|
||
| $('[itemscope]') | ||
| .filter(function () { | ||
| return $(this).parentsUntil('body', '[itemscope]').length === 0; | ||
| }) | ||
| .each(function () { | ||
| items.push(extractItem(this)); | ||
| }); | ||
|
|
||
| return items; | ||
| }; | ||
|
|
||
| return extractAllItems(); | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not entirely correct as per the microdata specification. See point 5.2.4 Values - the type of the value extracted is based on the element type.