PINGDOM_CHECK

#ExtractSummit2026 The world's largest web scraping conference returns. Austin Oct 7–8 · Dublin Nov 10–11.

Register now
Data Services
Pricing
Login
Try Zyte APIContact Sales
  • Unblocking and Extraction

    Zyte API

    The ultimate API for web scraping. Avoid website bans and access a headless browser or AI Parsing

    Ban Handling

    Headless Browser

    AI Extraction

    SERP

    Enterprise

    DocumentationSupport

    Hosting and Deployment

    Scrapy Cloud

    Run, monitor, and control your Scrapy spiders however you want to.

    Coding Agent Add-Ons

    Agentic Web Data

    Plugins that give coding agents the context to build production Scrapy projects. Starts with Claude Code.

  • Data Services
  • Pricing
  • Browse

    • BlogArticles, podcasts, videos
    • Case studiesCustomer outcomes
    • White papersIn-depth reports
    • EventsConferences, webinars, recordings

    Subscribe

    • NewsletterSwiftly delivered
    • Discord communityExtract Data community
  • Product and E-commerce

    From e-commerce and online marketplaces

    Data for AI

    Collect and structure web data to feed AI

    Job Posting

    From job boards and recruitment websites

    Real Estate

    From Listings portals and specialist websites

    News and Article

    From online publishers and news websites

    Search

    Search engine results page data (SERP)

    Social Media

    From social media platforms online

  • Meet Zyte

    Our story, people and values

    Contact us

    Get in touch

    Support

    Knowledge base and raise support tickets

    Terms and Policies

    Accept our terms and policies

    Open Source

    Our open source projects and contributions

    Web Data Compliance

    Guidelines and resources for compliant web data collection

    Join the team building the future of web data
    We're Hiring
    Trust Center
    Security, compliance & certifications
Login
Try Zyte APIContact Sales

Zyte Developers

Coding tools & hacks straight to your inbox

Become part of the community and receive a bi-weekly dosage of all things code.

Join us
    • Zyte Data
    • News & Articles
    • Search
    • Social Media
    • Product
    • Data for AI
    • Job Posting
    • Real Estate
    • Zyte API - Ban Handling
    • Zyte API - Headless Browser
    • Zyte API - AI Extraction
    • Web Scraping Copilot
    • Zyte API Enterprise
    • Scrapy Cloud
    • Solution Overview
    • Blog
    • Webinars
    • Case Studies
    • White Papers
    • Documentation
    • Web Scraping Maturity Self-Assesment
    • Web Data compliance
    • Meet Zyte
    • Jobs
    • Terms and Policies
    • Trust Center
    • Support
    • Contact us
    • Pricing
    • Do not sell
    • Cookie settings
    • Sign up
    • Talk to us
    • Cost estimator
All articles
AI60, 60 articles
Data quality13, 13 articles
Developer interest57, 57 articles
Integration2, 2 articles
Open-source40, 40 articles
Proxies29, 29 articles
Scraping practice17, 17 articles
Scraping strategy26, 26 articles
Web data60, 60 articles
Web scraping APIs33, 33 articles
Zyte API59, 59 articles
Scrapy48, 48 articles
Scrapy Cloud10, 10 articles
Web Scraping Copilot12, 12 articles
AI & Machine Learning1, 1 articles
Automotive2, 2 articles
E-commerce & retail26, 26 articles
Entertainment & Streaming2, 2 articles
Financial Services8, 8 articles
Government2, 2 articles
Market Research & Intelligence3, 3 articles
Media & publishing8, 8 articles
Real Estate2, 2 articles
Recruitment & HR3, 3 articles
Transportation & Logistics2, 2 articles
Travel & hospitality2, 2 articles
Extract Summit25, 25 articles
PyCon1, 1 articles

Appearance

Discord Community
BlogHow ToExtract Schema.Org Microdata with Scrapy Selectors
ArticleHow To

Extract Schema.Org Microdata with Scrapy Selectors

Web pages are full of data. Microdata markup helps machines understand pages. Schema.org supports a set of schemas for structured data markup on web pages.

V

Valdir Stumm Junior

5 min read · June 18, 2014

Extract Schema.Org Microdata with Scrapy Selectors

Extracting schema.org microdata using Scrapy selectors and XPath

We have released an lxml-based version of this code as an open-source library called extruct. The Source code is on Github, and the package is available on PyPI. Enjoy!


Web pages are full of data, that is what web scraping is mostly about. But often you want more than data, you want meaning. Microdata markup embedded in HTML source helps machines understand what the pages are about: contact information, product reviews, events, etc.

Web authors have several ways to add metadata to their web pages: HTML "meta" tags, microformats, social media meta tags (Facebook's Open Graph protocol, Twitter Cards).

Applying Schema.org

Then there is Schema.org, an initiative from big players in the search space (Yahoo!, Google, Bing, and Yandex) to “create and support a common set of schemas for structured data markup on web pages.”

Here are some stats from the Web Data Commons project on the use of microdata in the Common Crawl web corpus:

In summary, we found structured data within 585 million HTML pages out of the 2.24 billion pages contained in the crawl (26%).

Let's focus on Schema.org syntax for the rest of this article. The markup looks like this (example from schema.org "Getting started" page):

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

Avatar

<span>Director: <span itemprop="director">James Cameron</span> (born August 16, 1954)</span>

<span itemprop="genre">Science fiction</span>

<a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>

</div>

Avatar

Director: James Cameron (born August 16, 1954) Science fiction Trailer

Let's assume you want to extract this microdata and get the movie item from the snippet above.

Using Scrapy Selector let's first loop on elements with an itemscope attribute (this represents a container for an item's properties) using .//*[@itemscope], and for each item, get all properties, i.e. elements that have an itemprop attribute:

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> from scrapy.selector import Selector

>>> selector = Selector(text="""

...

http://schema.org/Movie

">

...

Avatar

... Director: James Cameron (born August 16, 1954)

... Science fiction

... Trailer

...

""", type="html")

>>> selector.xpath('.//*[@itemscope]')

[<Selector xpath='.//*[@itemscope]' data=u' <div itemscope itemtype="http://schema.o'\>\] >>>

>>> from scrapy.selector import Selector >>> selector = Selector(text=""" ...

...

Avatar

... Director: James Cameron (born August 16, 1954) ... Science fiction ... Trailer ...
""", type="html") >>> selector.xpath('.//*[@itemscope]') [<Selector xpath='.//*[@itemscope]' data=u' <div itemscope itemtype="http://schema.o'>\] >>>

Let's print out more interesting stuff, like the item's itemtype and the properties' values (the text representation of their HTML element):

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> for item in selector.xpath('.//*[@itemscope]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... [u'http://schema.org/Movie'\] [u'name'] [u'Avatar'] [u'director'] [u'James Cameron'] [u'genre'] [u'Science fiction'] [u'trailer'] [u'Trailer'] >>>

>>> for item in selector.xpath('.//*[@itemscope]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... [u'http://schema.org/Movie'\] [u'name'] [u'Avatar'] [u'director'] [u'James Cameron'] [u'genre'] [u'Science fiction'] [u'trailer'] [u'Trailer'] >>>

Hm. The value for the trailer isn't that interesting. We should have selected the href of <a href="../movies/avatar-theatrical-trailer.html">Trailer</a>.

For the following extended example markup, taken from Wikipedia,

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

Avatar
Director: James Cameron (born August 16, 1954) Science fiction Trailer

Avatar

Director: James Cameron (born August 16, 1954)
Science fiction
Trailer

we see it's not always href attributes that are interesting, but datetime perhaps, or src for images, or content for meta element like ``... any attribute really.

Therefore we need to get itemprop text content and elements attributes (we use the @* XPath expression for that).

Let's do that on the 2nd HTML snippet:

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> selector = Selector(text=""" ...

...

Avatar

...
... Director: James Cameron ... (born August 16, 1954) ...
... Science fiction ... Trailer ...
""", type="html") >>> for item in selector.xpath('.//*[@itemscope]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract(), ... print property.xpath('@*').extract() ... [u'http://schema.org/Movie'\] [u'name'] [u'Avatar'] [u'name'] [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] [u'director', u'', u'http://schema.org/Person'\] [u'name'] [u'James Cameron'] [u'name'] [u'birthDate'] [u'August 16, 1954'] [u'birthDate', u'1954-08-16'] [u'genre'] [u'Science fiction'] [u'genre'] [u'trailer'] [u'Trailer'] [u'../movies/avatar-theatrical-trailer.html', u'trailer'] [u'http://schema.org/Person'\] [u'name'] [u'James Cameron'] [u'name'] [u'birthDate'] [u'August 16, 1954'] [u'birthDate', u'1954-08-16'] >>>

>>> selector = Selector(text=""" ...

...

Avatar

...
... Director: James Cameron ... (born August 16, 1954) ...
... Science fiction ... Trailer ...
""", type="html") >>> for item in selector.xpath('.//*[@itemscope]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract(), ... print property.xpath('@*').extract() ... [u'http://schema.org/Movie'\] [u'name'] [u'Avatar'] [u'name'] [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] [u'director', u'', u'http://schema.org/Person'\] [u'name'] [u'James Cameron'] [u'name'] [u'birthDate'] [u'August 16, 1954'] [u'birthDate', u'1954-08-16'] [u'genre'] [u'Science fiction'] [u'genre'] [u'trailer'] [u'Trailer'] [u'../movies/avatar-theatrical-trailer.html', u'trailer'] [u'http://schema.org/Person'\] [u'name'] [u'James Cameron'] [u'name'] [u'birthDate'] [u'August 16, 1954'] [u'birthDate', u'1954-08-16'] >>>

Wait a minute! We're getting only attribute values, not attribute names (and itemprop attribute twice for that matter, once for @itemprop and once for @*).

To get names of attributes, one cannot apparently use name() on an attribute... BUT you can do name(@*[i]), i being the attribute position:

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print "Property:", ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@*[%d])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: [u'http://schema.org/Movie'\] Property: [u'name'] [u'Avatar'] attribute: name=[u'itemprop']; value=name Property: [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] attribute: name=[u'itemprop']; value=director attribute: name=[u'itemscope']; value= attribute: name=[u'itemtype']; value=http://schema.org/Person Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 Property: [u'genre'] [u'Science fiction'] attribute: name=[u'itemprop']; value=genre Property: [u'trailer'] [u'Trailer'] attribute: name=[u'href']; value=../movies/avatar-theatrical-trailer.html attribute: name=[u'itemprop']; value=trailer Item: [u'http://schema.org/Person'\] Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 >>>

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath('.//*[@itemprop]'): ... print "Property:", ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@*[%d])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: [u'http://schema.org/Movie'\] Property: [u'name'] [u'Avatar'] attribute: name=[u'itemprop']; value=name Property: [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] attribute: name=[u'itemprop']; value=director attribute: name=[u'itemscope']; value= attribute: name=[u'itemtype']; value=http://schema.org/Person Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 Property: [u'genre'] [u'Science fiction'] attribute: name=[u'itemprop']; value=genre Property: [u'trailer'] [u'Trailer'] attribute: name=[u'href']; value=../movies/avatar-theatrical-trailer.html attribute: name=[u'itemprop']; value=trailer Item: [u'http://schema.org/Person'\] Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 >>>

There's still something wrong, right? Duplicate properties.

It's because the 2nd HTML snippet is using embedded items. Indeed, James Cameron is the director of "Avatar", but he's also a Person (yes, he is!). That's why the markup says <div> with an itemscope attribute.

How can we fix that, only selecting properties at the current scope, and leave nested properties when we reach the nested item?

Well, it happens that Scrapy Selectors support some EXSLT extensions, notably the sets operations. Here, we'll use set:difference: we'll select itemprops under the current itemscope, and then exclude those that are themselves, children of another, itemscope, under the current one.

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath( ... """set:difference(.//*[@itemprop], ... .//*[@itemscope]//*[@itemprop])"""): ... print "Property:", property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@*[%d])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: [u'http://schema.org/Movie'\] Property: [u'name'] [u'Avatar'] attribute: name=[u'itemprop']; value=name Property: [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] attribute: name=[u'itemprop']; value=director attribute: name=[u'itemscope']; value= attribute: name=[u'itemtype']; value=http://schema.org/Person Property: [u'genre'] [u'Science fiction'] attribute: name=[u'itemprop']; value=genre Property: [u'trailer'] [u'Trailer'] attribute: name=[u'href']; value=../movies/avatar-theatrical-trailer.html attribute: name=[u'itemprop']; value=trailer Item: [u'http://schema.org/Person'\] Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 >>>

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath( ... """set:difference(.//*[@itemprop], ... .//*[@itemscope]//*[@itemprop])"""): ... print "Property:", property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@*[%d])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: [u'http://schema.org/Movie'\] Property: [u'name'] [u'Avatar'] attribute: name=[u'itemprop']; value=name Property: [u'director'] [u'n Director: James Cameron n(born August 16, 1954)n '] attribute: name=[u'itemprop']; value=director attribute: name=[u'itemscope']; value= attribute: name=[u'itemtype']; value=http://schema.org/Person Property: [u'genre'] [u'Science fiction'] attribute: name=[u'itemprop']; value=genre Property: [u'trailer'] [u'Trailer'] attribute: name=[u'href']; value=../movies/avatar-theatrical-trailer.html attribute: name=[u'itemprop']; value=trailer Item: [u'http://schema.org/Person'\] Property: [u'name'] [u'James Cameron'] attribute: name=[u'itemprop']; value=name Property: [u'birthDate'] [u'August 16, 1954'] attribute: name=[u'itemprop']; value=birthDate attribute: name=[u'datetime']; value=1954-08-16 >>>

But wouldn't it be nice to keep a reference to the Person item as property of the Movie director itemprop?

We'd need to uniquely identify items in the markup, maybe the position of each itemscope element, or its number.

You can use XPath count() function for that, counting other itemscopes that are siblings before (preceding::*[@itemscope]) or ancestors of the current one (ancestor::*[@itemscope]):

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::*[@itemscope]) ... + count(ancestor::*[@itemscope]) ... + 1""").extract() ... Item: [u'http://schema.org/Movie'\] ID: [u'1.0'] Item: [u'http://schema.org/Person'\] ID: [u'2.0'] >>>

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::*[@itemscope]) ... + count(ancestor::*[@itemscope]) ... + 1""").extract() ... Item: [u'http://schema.org/Movie'\] ID: [u'1.0'] Item: [u'http://schema.org/Person'\] ID: [u'2.0'] >>>

Here's a cleaned-up example routine, referencing items by their ID when itemprops are also itemscopes:

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::*[@itemscope]) ... + count(ancestor::*[@itemscope]) ... + 1""").extract() ... Item: [u'http://schema.org/Movie'\] ID: [u'1.0'] Item: [u'http://schema.org/Person'\] ID: [u'2.0'] >>>

>>> for item in selector.xpath('.//*[@itemscope]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::*[@itemscope]) ... + count(ancestor::*[@itemscope]) ... + 1""").extract() ... Item: [u'http://schema.org/Movie'\] ID: [u'1.0'] Item: [u'http://schema.org/Person'\] ID: [u'2.0'] >>>

This dict output is not quite what W3C details in the microdata specs, but it's close enough to leave the rest to you as an exercise 😉

You can find a Github Gist of the above code here.

Try Zyte API

Build your first scraper in minutes

Free trial, no credit card. From a single request to production in an afternoon.

Get started
How To
V

Valdir Stumm Junior

More from this author

In this article

  • Applying Schema.org

Follow

Get the latest

Zyte and the data web in your inbox — or wherever you already are.

Subscribe

Or follow elsewhere

Continue reading

Teaching AI to scrape like a pro: how we measure LLMs’ data quality
How To

Teaching AI to scrape like a pro: how we measure LLMs’ data quality

AI-enabled code editors can now conjure scraping code on command. But is it any good? Here’s how Zyte re-engineered LLMs with Web Scraping Copilot to drive best-in-class output.

Theresia Tanzil·10 min·February 23, 2026
Analyze web data quickly with Jupyter Notebooks and Zyte API
How To

Analyze web data quickly with Jupyter Notebooks and Zyte API

With AI Scraping in Zyte API, you can pull data from any e-commerce website straight into your Jupyter notebooks.

Neha Setia Nagpal·2 mins·December 13, 2024
Overcoming web scraping challenges of Puppeteer and Playwright
How To

Overcoming web scraping challenges of Puppeteer and Playwright

Discover the challenges of scaling web scraping with Playwright & Puppeteer, from browser farm management to IP rotation and anti-scraping tactics.

Neha Setia Nagpal·1 mins·December 5, 2024

The Community · Newsletter

The best of Zyte and the data web, in your inbox.

One curated edition — new articles, product updates, and the stories shaping the data web. No noise.

G2.com

Capterra.com

Proxyway.com

EWDCI logoMost loved workplace certificateZyte rewardISO 27001 iconG2 rewardG2 rewardG2 reward

© Zyte Group Limited 2026
1<div itemscope itemtype ="http://schema.org/Movie">
2<h1 itemprop="name">Avatar</h1>
3   <span>Director: <span itemprop="director">James Cameron</span> (born August 16, 1954)</span>
4   <span itemprop="genre">Science fiction</span>
5   <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
6</div>
Copy
1\>>> from scrapy.selector import Selector
2>>> selector = Selector(text="""
3...
4
5
6<div itemscope itemtype ="
7http://schema.org/Movie
8">
9...
10
11
12<h1 itemprop="name">Avatar</h1>
13
14
15...   <span>Director: <span itemprop="director">James Cameron</span> (born August 16, 1954)</span>
16...   <span itemprop="genre">Science fiction</span>
17...  <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
18... </div>
19
20
21""", type="html")
22>>> selector.xpath('.//\*\[@itemscope\]')
23\[<Selector xpath='.//\*\[@itemscope\]' data=u' <div itemscope itemtype="http://schema.o'>\] >>>
Copy
1\>>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//\*\[@itemprop\]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... \[u'http://schema.org/Movie'\] \[u'name'\] \[u'Avatar'\] \[u'director'\] \[u'James Cameron'\] \[u'genre'\] \[u'Science fiction'\] \[u'trailer'\] \[u'Trailer'\] >>>
Copy
1<div itemscope itemtype="http://schema.org/Movie"> <h1 itemprop="name">Avatar</h1> <div itemprop="director" itemscope itemtype="http://schema.org/Person"> Director: <span itemprop="name">James Cameron</span> (born <time itemprop="birthDate" datetime="1954-08-16">August 16, 1954</time>) </div> <span itemprop="genre">Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a> </div>
Copy
1\>>> selector = Selector(text=""" ... <div itemscope itemtype=" http://schema.org/Movie "> ... <h1 itemprop="name">Avatar</h1> ... <div itemprop="director" itemscope itemtype=" http://schema.org/Person "> ... Director: <span itemprop="name">James Cameron</span> ... (born <time itemprop="birthDate" datetime="1954-08-16">August 16, 1954</time>) ... </div> ... <span itemprop="genre">Science fiction</span> ... <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a> ... </div> """, type="html") >>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print item.xpath('@itemtype').extract() ... for property in item.xpath('.//\*\[@itemprop\]'): ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract(), ... print property.xpath('@\*').extract() ... \[u'http://schema.org/Movie'\] \[u'name'\] \[u'Avatar'\] \[u'name'\] \[u'director'\] \[u'n Director: James Cameron n(born August 16, 1954)n '\] \[u'director', u'', u'http://schema.org/Person'\] \[u'name'\] \[u'James Cameron'\] \[u'name'\] \[u'birthDate'\] \[u'August 16, 1954'\] \[u'birthDate', u'1954-08-16'\] \[u'genre'\] \[u'Science fiction'\] \[u'genre'\] \[u'trailer'\] \[u'Trailer'\] \[u'../movies/avatar-theatrical-trailer.html', u'trailer'\] \[u'http://schema.org/Person'\] \[u'name'\] \[u'James Cameron'\] \[u'name'\] \[u'birthDate'\] \[u'August 16, 1954'\] \[u'birthDate', u'1954-08-16'\] >>>
Copy
1\>>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath('.//\*\[@itemprop\]'): ... print "Property:", ... print property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@\*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@\*\[%d\])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: \[u'http://schema.org/Movie'\] Property: \[u'name'\] \[u'Avatar'\] attribute: name=\[u'itemprop'\]; value=name Property: \[u'director'\] \[u'n Director: James Cameron n(born August 16, 1954)n '\] attribute: name=\[u'itemprop'\]; value=director attribute: name=\[u'itemscope'\]; value= attribute: name=\[u'itemtype'\]; value=http://schema.org/Person Property: \[u'name'\] \[u'James Cameron'\] attribute: name=\[u'itemprop'\]; value=name Property: \[u'birthDate'\] \[u'August 16, 1954'\] attribute: name=\[u'itemprop'\]; value=birthDate attribute: name=\[u'datetime'\]; value=1954-08-16 Property: \[u'genre'\] \[u'Science fiction'\] attribute: name=\[u'itemprop'\]; value=genre Property: \[u'trailer'\] \[u'Trailer'\] attribute: name=\[u'href'\]; value=../movies/avatar-theatrical-trailer.html attribute: name=\[u'itemprop'\]; value=trailer Item: \[u'http://schema.org/Person'\] Property: \[u'name'\] \[u'James Cameron'\] attribute: name=\[u'itemprop'\]; value=name Property: \[u'birthDate'\] \[u'August 16, 1954'\] attribute: name=\[u'itemprop'\]; value=birthDate attribute: name=\[u'datetime'\]; value=1954-08-16 >>>
Copy
1\>>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print "Item:", item.xpath('@itemtype').extract() ... for property in item.xpath( ... """set:difference(.//\*\[@itemprop\], ... .//\*\[@itemscope\]//\*\[@itemprop\])"""): ... print "Property:", property.xpath('@itemprop').extract(), ... print property.xpath('string(.)').extract() ... for position, attribute in enumerate(property.xpath('@\*'), start=1): ... print "attribute: name=%s; value=%s" % ( ... property.xpath('name(@\*\[%d\])' % position).extract(), ... attribute.extract()) ... print ... print ... Item: \[u'http://schema.org/Movie'\] Property: \[u'name'\] \[u'Avatar'\] attribute: name=\[u'itemprop'\]; value=name Property: \[u'director'\] \[u'n Director: James Cameron n(born August 16, 1954)n '\] attribute: name=\[u'itemprop'\]; value=director attribute: name=\[u'itemscope'\]; value= attribute: name=\[u'itemtype'\]; value=http://schema.org/Person Property: \[u'genre'\] \[u'Science fiction'\] attribute: name=\[u'itemprop'\]; value=genre Property: \[u'trailer'\] \[u'Trailer'\] attribute: name=\[u'href'\]; value=../movies/avatar-theatrical-trailer.html attribute: name=\[u'itemprop'\]; value=trailer Item: \[u'http://schema.org/Person'\] Property: \[u'name'\] \[u'James Cameron'\] attribute: name=\[u'itemprop'\]; value=name Property: \[u'birthDate'\] \[u'August 16, 1954'\] attribute: name=\[u'itemprop'\]; value=birthDate attribute: name=\[u'datetime'\]; value=1954-08-16 >>>
Copy
1\>>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::\*\[@itemscope\]) ... + count(ancestor::\*\[@itemscope\]) ... + 1""").extract() ... Item: \[u'http://schema.org/Movie'\] ID: \[u'1.0'\] Item: \[u'http://schema.org/Person'\] ID: \[u'2.0'\] >>>
Copy
1\>>> for item in selector.xpath('.//\*\[@itemscope\]'): ... print "Item:", item.xpath('@itemtype').extract() ... print "ID:", item.xpath("""count(preceding::\*\[@itemscope\]) ... + count(ancestor::\*\[@itemscope\]) ... + 1""").extract() ... Item: \[u'http://schema.org/Movie'\] ID: \[u'1.0'\] Item: \[u'http://schema.org/Person'\] ID: \[u'2.0'\] >>>
Copy