Skip to main content

A python API wrapper for blocket.se

Project description

BlocketAPI

PyPI version License Python versions PyPI - Downloads

BlocketAPI allows users to query saved searches, known as "Bevakningar", on blocket.se. This means you can either retrieve results from a specific saved search or list all listings/ads across all saved searches. The results from these queries are returned in a json format.

Blocket is one of Sweden's largest online marketplaces. It was founded in 1996 and allows users to buy and sell a wide range of items, including cars, real estate, jobs, services, and second-hand goods. The platform is known for its extensive reach and user-friendly interface, making it a popular choice for Swedes looking to purchase or sell items quickly and efficiently.

✨ Features

  • List saved searches, called "Bevakningar".
  • Get listings from all or a specific saved search.
  • Search for anything on Blocket with filters for region and category.
  • Get vehicle information, price evaluation and search for vehicles with multiple filters.
  • Search for homes with filters for city, home type and more.
  • Search for stores and get a specific store's listings.

🧑‍💻️ Install

BlocketAPI is available on PyPI.

pip install blocket-api

💁‍♀️ Usage

>>> from blocket_api import BlocketAPI
>>> api = BlocketAPI("YourBlocketTokenHere")
>>> print(api.saved_searches())
...
>>> print(BlocketAPI().custom_search("saab")) # no token required
...

Some calls require a bearerToken. However, some calls are public and don't require a token.

Where token?

Function Token required Description
api.saved_searches() 🔐 Yes List your saved searches (bevakningar)
api.get_listings() 🔐 Yes List items related to a saved search
api.custom_search() 👏 No Search for everything on Blocket and filter by region
api.motor_search() 👏 No Advanced search for car-listings.
api.price_eval() 👏 No Vehicle purchase valuation and details.
api.home_search() 👏 No Query home listings.
api.store_search() 👏 No Search for a store.
api.get_store_listings() 👏 No Get listings from a specific store.

🤓 Detailed usage

saved_searches()

Saved searches are your so called "Bevakningar" and can be found here. Each saved search has and unique id which can be used as a parameter to get_listings(), see below.

>>> api.saved_searches()
[
   {
      "id":"4150081",
      "new_count":0,
      "total_count":41,
      "push_enabled":false,
      "push_available":true,
      "query":"cg=1020&q=buggy&st=s",
      "name":"\"buggy\", Bilar säljes i hela Sverige"
   },
]

get_listings(search_id, limit)

Returns all listings related to a saved search.

Parameters:

  • search_id (int, optional) - Get listings for a specific saved search. If not provided, all saved searches will be combined.
  • limit (int, optional) - Limit number of results returned, max is 99.
>>> api.get_listings(4150081)
{
   "data":[
      {
         "ad":{
            "ad_id":"1401053984",
            "list_id":"1401053984",
            "zipcode":"81290",
            "ad_status":"active",
            "list_time":"2024-07-15T19:07:16+02:00",
            "subject":"Volkswagen 1500 lim 113 chassi",
            "body":"Säljer ett chassi/bottenplatta till en volkswagen 1500 lim 113 1967, blästrat och målat.\nFinns en beach buggy kaross att få med om man vill det. \nReg nmr ABC123",
            "price":{
               "value":10000,
               "suffix":"kr"
            },
            ...
         },
      },
   ],
   "total_count":41,
   "timestamp":"2024-07-16T08:08:43.810828006Z",
   "total_page_count":1
}

custom_search(search_query, region, limit)

Make a custom search through out all of blocked. A region can be passed in as parameter for filtering.

Parameters:

  • search_query (str, required) - A string to search for.
  • region (enum, optional) - Filter results on a region, default is all of Sweden.
  • category (enum, optional) - Filter for a specific category, ex. Category.for_hemmet.
  • limit (int, optional) - Limit number of results returned, max is 99.
>>> from blocket_api import Region
>>> api.custom_search("saab", Region.blekinge) # search for term "saab" in region of "Blekinge"
{
   "data":[
      {
         "ad_id":"1401038836",
         "ad_status":"active",
         "advertiser":{
            "account_id":"684279",
            "name":"Stefan Ingves",
            "type":"private"
         },
         ...
         "location":[
            {
               "id":"22",
               "name":"Blekinge",
               "query_key":"r"
            },
            {
               "id":"256",
               "name":"Ronneby",
               "query_key":"m"
            }
         ],
         ...
      }
    ]
}

motor_search(page, make, fuel, chassi, price, modelYear, milage, gearbox)

To query listings related to a specific car, supply the following parameters:

  • page (int, required) - Results are split in pages, set page number here.
  • make (List[MAKE_OPTIONS]) - Filter a specific make, ex. Audi.
  • fuel (Optional[List[FUEL_OPTIONS]]) - Filter a specific fuel, ex. Diesel.
  • chassi (Optional[List[CHASSI_OPTIONS]]) - Filter a specific chassi, ex. Cab.
  • price (Optional[Tuple[int, int]]) - Set price range, ex. (50000, 100000).
  • modelYear (Optional[Tuple[int, int]]) - Set model year range, ex. (2000, 2020).
  • milage (Optional[Tuple[int, int]]) - Set milage range, ex. (1000, 2000).
  • gearbox (Optional[GEARBOX_OPTIONS]) - Filter a specific gearbox, ex. Automat.
>>> api.motor_search(
    make=["Audi", "Ford"],
    fuel=["Diesel"],
    chassi=["Cab"],
    price=(50000, 100000),
    page=1,
)
...

price_eval(registration_number)

Query price evaluation for a specific vehicle by using cars registration number (ABC123). This returns company and private estimated prices, car information, and more. The api queries same endpoint as Blockets "värdera bil" service.

  • registration_number (str, required) - Registration number of the vehicle.
>>> api.price_eval("ABC123")
{
   "registration_number": "ABC123",
   "private_valuation": 108155,
   "company_valuation": 108155,
   "car_info": {
      "make": "Volkswagen",
      "model": "Polo"
      ...
   }
   ...
}

home_search(city, type, order_by, ordering, offset)

Query home listings from bostad.blocket.se.

  • city (str, required) - City name, ex. Stockholm.
  • type (HomeType, optional) - Type of home, ex. HomeType.apartment.
  • order_by (OrderBy, optional) - Sorting order, ex. OrderBy.price.
  • ordering (HOME_SEARCH_ORDERING, optional) - Sorting order, ex. "descending".
  • offset (int, optional) - Offset for results, ex. 60.
>>> from blocket_api.qasa import HomeType, OrderBy
>>> api.home_search(
    city="Stockholm",
    type=HomeType.apartment,
    order_by=OrderBy.price,
    ordering="descending",
)
...

store_search(search_query, page)

Search for a store in Blocket stores from blocket.se/butiker.

  • search_query (str, required) - The name of the store.
  • page (int, optional) - The page number to return.
>>> api.store_search("Jannes Car and Kebab")
{
  "data": [
    {
      "store_id": "1234",
      "store_name": "Jannes Car and Kebab",
      ...
    },
    ...
  ]
}

get_store_listings(store_id, page)

Get listings from a specific store.

  • store_id (int, required) - The store id. Can be obtained by calling store_search().
  • page (int, optional) - The page number to return.
>>> api.get_store_listings(1234)
{
  "data": [
    {
      "ad_id": "1234",
      "price": 316000,
      "body": "A very nice car.",
      "images": [],
      ...
    },
    ...
  ]
}

🔐 Blocket API token

There are two ways to acquire your token:

  • Log in to Blocket.se with your credentials using any web browser.
  • Go to this URL and copy the value of bearerToken.

If there's a better way of doing this, feel free to help out in #2.

Your token can also be found in the request headers in the "Bevakningar"-section on Blocket.

  • Login to blocket.se: Sign in with your credentials.
  • Click "Bevakningar": Go to the "Bevakningar" section.
  • Inspect the page: Right-click the page and select "Inspect".
  • Open the Network tab: Switch to the Network tab in the Developer Tools.
  • Find request headers: Locate a request where the domain is "api.blocket.se" and the file is "searches". Pretty much every request to api.blocket.se contains this auth-header, so any request will do.
  • Inspect request headers: Look at the request headers to find your token under "Authorization".

token

My token has never expired or changed during this project. However, if your're met with a 401 Unauthorized at some point, you may want to refresh your token by repeating the steps above.

📝 Notes

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

blocket_api-0.2.8.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blocket_api-0.2.8-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file blocket_api-0.2.8.tar.gz.

File metadata

  • Download URL: blocket_api-0.2.8.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for blocket_api-0.2.8.tar.gz
Algorithm Hash digest
SHA256 d7e0364f4256881a70a6072bfa7f3e7c1da49f143a4933478f3e207c9383e165
MD5 351c69c13a32add777172d072f2852d0
BLAKE2b-256 4170fa2af60d66ba69821e00ce54680f3aa037bdeba4f7889c09c10dd3e91a93

See more details on using hashes here.

File details

Details for the file blocket_api-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: blocket_api-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for blocket_api-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 16ade23c34ad3cadc65a862fdad096c277d15bc8eb058a842d548a85032c037e
MD5 9c60316f28e63377c8652c23f857ea6e
BLAKE2b-256 1581912975031765ef8e2a107da35122340e59628db9e3157f407ccb4a9bf0f3

See more details on using hashes here.

Supported by

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