Skip to main content

Scrapinghub's Page Object pattern for web scraping

Project description

PyPI Version Supported Python Versions Build Status Coverage report

web-poet implements Page Object pattern for web scraping.

License is BSD 3-clause.

Installation

pip install web-poet

Usage

Check the following script that uses urllib.request to query data from books.toscrape.com.

import urllib.request

from web_poet.pages import ItemWebPage
from web_poet.page_inputs import ResponseData


class BookLinksPage(ItemWebPage):

    @property
    def links(self):
        return self.css('.image_container a::attr(href)').getall()

    def to_item(self) -> dict:
        return {
            'links': self.links,
        }

response = urllib.request.urlopen('http://books.toscrape.com')
response_data = ResponseData(response.url, response.read().decode('utf-8'))
page = BookLinksPage(response_data)

print(page.to_item())

Output should be similar to this:

{
    'links': [
        'catalogue/a-light-in-the-attic_1000/index.html',
        'catalogue/tipping-the-velvet_999/index.html',
        'catalogue/soumission_998/index.html',
        'catalogue/sharp-objects_997/index.html',
        'catalogue/sapiens-a-brief-history-of-humankind_996/index.html',
        'catalogue/the-requiem-red_995/index.html',
        'catalogue/the-dirty-little-secrets-of-getting-your-dream-job_994/index.html',
        'catalogue/the-coming-woman-a-novel-based-on-the-life-of-the-infamous-feminist-victoria-woodhull_993/index.html',
        'catalogue/the-boys-in-the-boat-nine-americans-and-their-epic-quest-for-gold-at-the-1936-berlin-olympics_992/index.html',
        'catalogue/the-black-maria_991/index.html',
        'catalogue/starving-hearts-triangular-trade-trilogy-1_990/index.html',
        'catalogue/shakespeares-sonnets_989/index.html',
        'catalogue/set-me-free_988/index.html',
        'catalogue/scott-pilgrims-precious-little-life-scott-pilgrim-1_987/index.html',
        'catalogue/rip-it-up-and-start-again_986/index.html',
        'catalogue/our-band-could-be-your-life-scenes-from-the-american-indie-underground-1981-1991_985/index.html',
        'catalogue/olio_984/index.html',
        'catalogue/mesaerion-the-best-science-fiction-stories-1800-1849_983/index.html',
        'catalogue/libertarianism-for-beginners_982/index.html',
        'catalogue/its-only-the-himalayas_981/index.html',
    ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web-poet-0.0.1.tar.gz (17.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page