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
BlogProduct UpdateIntroducing Portia2Code: Transforming Portia Projects into Scrapy Spiders
ArticleProduct announcementProduct Update

Introducing Portia2Code: Transforming Portia Projects into Scrapy Spiders

Introducing Portia2Code: Seamlessly integrate Portia projects into Scrapy spiders with our latest guide, unlocking new possibilities for efficient web scraping.

V

Valdir Stumm Junior

3 min read · June 29, 2016

Introducing Portia2Code: Transforming Portia Projects into Scrapy Spiders

Introducing Portia2Code: Portia projects into Scrapy spiders

Note: Portia is no longer available for new users. It has been disabled for all the new organisations from August 20, 2018 onward.

We’re thrilled to announce the release of our latest tool, Portia2Code!

port3

With it you can convert your Portia 2.0 projects into Scrapy spiders. This means you can add your own functionality and use Portia’s friendly UI to quickly prototype your spiders, giving you much more control and flexibility.

A perfect example of where you may find this new feature useful is when you need to interact with the web page. You can convert your Portia project to Scrapy, and then use Splash with a custom script to close pop-ups, scroll for more results, fill in forms, and so on.

Read on to learn more about using Portia2Code and how it can fit in your stack. But keep in mind that it only supports Portia 2.0 projects.

Using Portia2Code

First you need to install the portia2code library using:

1$ pip install portia2code
Copy

Then you need to download and extract your Portia project. You can do this through the API:

1$ curl --user $SHUB\_APIKEY:  'https://portia-beta.scrapinghub.com/api/projects/$PROJECT\_ID/download' > project.zip $ unzip project.zip -d project
Copy

Finally, you can convert your project with:

1$ portia\_porter project converted\_output\_dir
Copy

Customising Your Spiders

You can change the functionality as you would with a standard Scrapy spider. Portia2code produces spiders that extend from scrapy.CrawlSpider, the code for which is included in the downloaded project.

The example below shows you how to make an additional API request when there’s a meta property on the page named ‘metrics’.

In this example, the extended spider is separated out from the original spider. This is to demonstrate the changes that you need to make when modifying the spider. In practice you would make changes to the spider in the same class.

1`from scrapy.linkextractors import LinkExtractor from scrapy.spiders import Rule from ..utils.spiders import BasePortiaSpider from ..utils.processors import Field from ..utils.processors import Item from ..items import ArticleItem class ExampleCom(BasePortiaSpider): name = "www.example.com" start_urls = [u'http://www.example.com/search/?q=articles'] allowed_domains = [u'www.example.com'] rules = [ Rule(LinkExtractor(allow=(ur'd{6}'), deny=()), callback='parse_item', follow=True) ] items = [ [Item(ArticleItem, None, u'#content', [ Field(u'title', u'.page_title *::text', []), Field(u'Article', u'.article *::text', []), Field(u'published', u'.date *::text', []), Field(u'Authors', u'.authors *::text', []), Field(u'pdf', u'#pdf-link::attr(href)', [])])] ] import json from scrapy import Request from six.moves.urllib.parse import urljoin class ExtendedExampleCom(ExampleCom): base_api_url = 'https://api.examplemetrics.com/v1/metrics/' allowed_domains = [u'www.example.com', u'api.examplemetrics.com'] def parse_item(self, response): for item in super(ExtendedExampleCom, self).parse_item(response): score = response.css('meta[name="metrics"]::attr(content)') if score: yield Request( url=urljoin(self.base_api_url, score.extract()[0]), callback=self.add_score, meta={'item': item}) else: yield item def add_score(self, response): item = response.meta['item'] item['score'] = json.loads(response.body)['score'] return item`
Copy

What's happening here?

The site contains a meta tag. We join its content attribute with the base URL given by base_api_url to produce the full URL for the metrics.

The domain of the base_api_url differs from the rest of the site. This means we have to add its domain to the allowed_domains array to prevent it from being filtered.

We want to add an extra field to the items extracted, so the first step is to override the parse_item function. The most important part is to loop over parse_item in the superclass in order to extract the items.

Next we need to check if the meta property ‘metrics’ is present. If that’s the case, we send another request and store the current item in the request meta. Once we receive a response, we use the add_score method that we defined to add the score property from the JSON response, and then return the final item. If the property is not present, we return the item as is.

This is a common pattern in Portia-built spiders. You would need to load some pages in Splash, which greatly increases the time to crawl a site. This approach means you can download the additional data with a single small request without having to load scripts and other assets on the page.

How it works

When you build a spider in Portia, the output consists largely of JSON definitions that define how the spider should crawl and extract data.

When you run a spider, the JSON definitions are compiled into a custom Scrapy spider along with trained samples for extraction. The spider uses the Scrapely library with the trained samples to extract from similar pages.

Portia uses unique selectors for each annotated element and builds an extraction tree that can use item loaders to extract the relevant data.

Future Plans

Here are the features that we are planning to add in the future:

  • Load pages using Splash depending on crawl rules
  • Follow links automatically
  • Text data extractors (annotations generated by highlighting text)

Wrap Up

We predict that Portia2Code will make even more useful to those of you who need to scrape data fast and efficiently. Let us know how you will use the new Portia2Code feature by Tweeting at us.

Happy scraping!

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
Product Update
V

Valdir Stumm Junior

More from this author

In this article

  • Using Portia2Code
  • Customising Your Spiders
  • What's happening here?
  • How it works
  • Future Plans
  • Wrap Up

Follow

Get the latest

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

Subscribe

Or follow elsewhere

Continue reading

Play Before You Scrape: Explore Zyte API Settings with Playground
Product Update

Play Before You Scrape: Explore Zyte API Settings with Playground

Discover the best way to configure your scrapers using Zyte API Playground

Cleber Alexandre·10 Mins·February 10, 2025
New in Zyte: Scroll Control, Lower Costs, and More
Product Update

New in Zyte: Scroll Control, Lower Costs, and More

As the web continues to evolve, Zyte API is evolving right alongside it—adding powerful new features and refinements designed to make data extraction smarter, faster, and more adaptable than ever.

Daniel Cave·5 min·June 27, 2025
From products to SERPs: AI scraping now does it all
Product Update

From products to SERPs: AI scraping now does it all

Scale data extraction with Zyte’s composite AI, combining accuracy, flexibility, and cost-efficiency in one powerful scraping solution, now available for the most common data types.

Cleber Alexandre·10 mins·April 3, 2025

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