Saturday, 29 October 2022

A Python script works fine on Windows but throws an error on Mac

I wish to get different product information from this webpage using requests module. I've created a script in Python to get a JSON response by issuing post requests with appropriate parameters.

The script works fine on windows but throws this error JSONDecodeError: Expecting value: line 1 column 1 (char 0) on mac.

Here is how I've tried:

import requests

start_url = 'https://www.jumbo.com/producten/'
link = 'https://www.jumbo.com/api/graphql'

payload = {"operation":"searchResult","variables":{"searchTerms":"","sortOption":"","showMoreIds":"","offSet":0,"pageSize":24,"categoryUrl":"/producten/"},"query":"\n  fragment productFields on Product {\n    id: sku\n    brand\n    badgeDescription\n    category\n    subtitle: packSizeDisplay\n    title\n    image\n    inAssortment\n    availability {\n      availability\n      isAvailable\n      label\n    }\n    isAvailable\n    isSponsored\n    link\n    status\n    retailSet\n    prices: price {\n      price\n      promoPrice\n      pricePerUnit {\n        price\n        unit\n      }\n    }\n    crossSellSkus\n    quantityDetails {\n      maxAmount\n      minAmount\n      stepAmount\n      defaultAmount\n      unit\n    }\n    quantityOptions {\n      maxAmount\n      minAmount\n      stepAmount\n      unit\n    }\n    primaryBadge: primaryBadges {\n      alt\n      image\n    }\n    secondaryBadges {\n      alt\n      image\n    }\n    promotions {\n      id\n      group\n      isKiesAndMix\n      image\n      tags {\n        text\n        inverse\n      }\n      start {\n        dayShort\n        date\n        monthShort\n      }\n      end {\n        dayShort\n        date\n        monthShort\n      }\n      attachments{\n        type\n        path\n      }\n    }\n  }\n\n  query searchResult(\n    $searchTerms: String\n    $filters: String\n    $offSet: Int\n    $showMoreIds: String\n    $sortOption: String\n    $pageSize: Int\n    $categoryUrl: String\n  ) {\n    searchResult(\n      searchTerms: $searchTerms\n      filters: $filters\n      offSet: $offSet\n      showMoreIds: $showMoreIds\n      sortOption: $sortOption\n      pageSize: $pageSize\n      categoryUrl: $categoryUrl\n    ) {\n      canonicalRelativePath\n      categoryIdPath\n      categoryTiles {\n        id\n        label\n        imageLink\n        navigationState\n        siteRootPath\n      }\n      urlState\n      newUrl\n      redirectUrl\n      shelfDescription\n      removeAllAction\n      powerFilters {\n        displayName\n        navigationState\n        siteRootPath\n      }\n      metaData {\n        title\n        description\n      }\n      headerContent {\n        headerText\n        count\n      }\n      helperText {\n        show\n        shortBody\n        longBody\n        header\n        linkText\n        targetUrl\n        messageType\n      }\n      recipeLink {\n        linkText\n        targetUrl\n        textIsRich\n      }\n      guidedNavigation {\n        ancestors {\n          label\n        }\n        displayName\n        dimensionName\n        groupName\n        name\n        multiSelect\n        moreLink {\n          label\n          navigationState\n        }\n        lessLink {\n          label\n          navigationState\n        }\n        refinements {\n          label\n          count\n          multiSelect\n          navigationState\n          siteRootPath\n        }\n      }\n      selectedRefinements {\n        refinementCrumbs {\n          label\n          count\n          multiSelect\n          dimensionName\n          ancestors {\n            label\n            navigationState\n          }\n          removeAction {\n            navigationState\n          }\n        }\n        searchCrumbs {\n         terms\n         removeAction {\n          navigationState\n         }\n        }\n        removeAllAction {\n         navigationState\n        }\n      }\n      socialLists {\n        title\n        totalNumRecs\n        lists {\n          id\n          title\n          followers\n          productImages\n          thumbnail\n          author\n          labels\n          isAuthorVerified\n        }\n      }\n      mainContent {\n        searchWarning\n        searchAdjustments {\n          originalTerms\n          adjustedSearches {\n            key\n            terms {\n              autoPhrased\n              adjustedTerms\n              spellCorrected\n            }\n          }\n        }\n      }\n      productsResultList {\n        pagingActionTemplate {\n          navigationState\n        }\n        lastRecNum\n        totalNumRecs\n        sortOptions {\n          navigationState\n          label\n          selected\n        }\n        products {\n          ...productFields\n          retailSetProducts {\n            ...productFields\n          }\n        }\n      }\n    }\n  }\n"}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
    'accept': 'application/json, text/plain, */*',
    'referer': 'https://www.jumbo.com/producten/',
    'origin': 'https://www.jumbo.com',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9,bn;q=0.8',
}

with requests.Session() as s:
    s.headers.update(headers)
    s.get(start_url)
    res = s.post(link,json=payload)
    print(res.json())

How can I make it work on the Mac?

This video demo represents how the script performs when I execute it on windows.



from A Python script works fine on Windows but throws an error on Mac

No comments:

Post a Comment