在 Python 中使用 API-请求?

Using API-request in Python?

我想使用来自此页面的 api-请求:

https://www.jamesallen.com/loose-diamonds/all-diamonds/page-4/?Color=Y,J,I,H,G,F,E,D&Cut=Good,Very+Good,Ideal,TrueHearts&Shape=all-diamonds&Clarity=I1,SI2,SI1,VS2,VS1,VVS2,VVS1,IF,FL&CaratFrom=0.05&IsLabDiamond=True

我想我找到了请求-url:

https://product-service.r2net.com/diamond/v/2/

在有效负载选项卡中,我还认为我找到了所有使用的参数。

所以我用下面的代码试了一下 -

但不幸的是,我在 vscode 中收到一个错误,即有效负载分配不正确 (第一个 { 下划线来自 payload =)

import requests
import pandas as pd

if __name__ == '__main__':
  headers =   {
      'appid':'jamesallen',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
      }
  payload = {
        searchByIDs: (price:{from:200, to:5000000} 
        carat:{from:0.05, to:30} 
        page:{count:5, size:23, number:6}depth:{from:46, to:78}tableSize:{from:52, to:80}color:{from: 1 to: 24} cut:{from: 0 to: 4} clarity:{from: 1 to: 9} isLabDiamond:true shippingDays:999 ) {    
          total
          hits
          pageNumber
          numberOfPages
              items {
                
    categories
    productID
    sku
    productType
    generalType
    isExpressShipping
    galleryUrl
    title
    description
    itemID
    sku
    shippingDate
    shippingDates{
    shippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    } invShippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    }}
    isFirmShipping
    price
    salePrice
    originalPrice
    url
    metaTitle
    metaDescription
    targetSite
    status {
      id
      name
    }
    media {
      thumb
      hearts
      arrows
      gallery
      gallerySet
      galleryDisplayType
      stageDisplayType
      segomaPhotoID
      tab
      stage
      stageSet
      supperZoom
      supperZoomSet
      sideView
      cert
      cart
    }
    stone {
      simpleColor
      species
      variety
      treatment
      ratio
      isTrueHearts
      isGemstonePair
      fancyColorOvertone
      measurements
      measureLength
      measureWidth
      isLabDiamond
      isArgyle
      carat
      shape {
        id
        name
      }
      color {
        id
        name
        isFancy
      }
      cut {
        id
        name
      }
      clarity {
        id
        name
      }
      lab {
        id
        name
      }
      flour {
        id
        name
        fullName
      }
      symmetry {
        id
        name
        fullName
      }
      polish {
        id
        name
        fullName
      }
      colorIntensity {
        id
        name
      }
      depth
      tableSize
    }
    
              }
          }
        }

  url = "https://product-service.r2net.com/diamond/v/2/"

  resp = requests.post(url,data=json.dumps(payload), headers=headers).json()
  print(resp)

如何在 python 中使用这个 api?

如前所述,您的有效负载变量无效 json。它还需要 "query" 键。

试一试:

import requests

headers =   {
      'appid':'jamesallen',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
      }
payload = {"query":"""{
        searchByIDs(price:{from:200, to:5000000} 
        carat:{from:0.05, to:30} 
        page:{count:5, size:23, number:6}depth:{from:46, to:78}tableSize:{from:52, to:80}color:{from: 1 to: 24} cut:{from: 0 to: 4} clarity:{from: 1 to: 9} isLabDiamond:true shippingDays:999 ) {    
          total
          hits
          pageNumber
          numberOfPages
              items {
                
    categories
    productID
    sku
    productType
    generalType
    isExpressShipping
    galleryUrl
    title
    description
    itemID
    sku
    shippingDate
    shippingDates{
    shippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    } invShippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    }}
    isFirmShipping
    price
    salePrice
    originalPrice
    url
    metaTitle
    metaDescription
    targetSite
    status {
      id
      name
    }
    media {
      thumb
      hearts
      arrows
      gallery
      gallerySet
      galleryDisplayType
      stageDisplayType
      segomaPhotoID
      tab
      stage
      stageSet
      supperZoom
      supperZoomSet
      sideView
      cert
      cart
    }
    stone {
      simpleColor
      species
      variety
      treatment
      ratio
      isTrueHearts
      isGemstonePair
      fancyColorOvertone
      measurements
      measureLength
      measureWidth
      isLabDiamond
      isArgyle
      carat
      shape {
        id
        name
      }
      color {
        id
        name
        isFancy
      }
      cut {
        id
        name
      }
      clarity {
        id
        name
      }
      lab {
        id
        name
      }
      flour {
        id
        name
        fullName
      }
      symmetry {
        id
        name
        fullName
      }
      polish {
        id
        name
        fullName
      }
      colorIntensity {
        id
        name
      }
      depth
      tableSize
    }
    
              }
          }
        }"""}

url = "https://product-service.r2net.com/diamond/v/2/"

resp = requests.post(url,json=payload, headers=headers).json()
print(resp)

输出:

print(resp)
{'data': {'searchByIDs': {'total': 44785, 'hits': 43633, 'pageNumber': 6, 'numberOfPages': 1898, 'items': [[{'categories': ['999'], 'productID': 12470344, 'sku': '12142391', 'productType': 'diamond', 'generalType': 'stone', 'isExpressShipping': False, 'galleryUrl': 'fancy-color-diamonds/yellow/', 'title': 'Lab-Created 0.40 Carat Fancy Vivid Yellow-VS1 Pear Shaped Diamond', 'description': 'This pear shaped 0.4 carat Lab-Created Fancy Vivid Yellow color vs1 clarity has a diamond grading report from IGI', 'itemID': 12142391, 'shippingDate': '2022-01-28T00:00:00.000Z', 'shippingDates': {'shippingDate': {'date': '2022-01-28T00:00:00.000Z', 'byValentines': True, 'byNewYears': False, 'byXmas': False, 'isFastShipping': True}, 'invShippingDate': None}, 'isFirmShipping': True, 'price': 290, 'salePrice': None, 'originalPrice': None, 'url': 'loose-diamonds/fancy-color/yellow/pear-shaped/0.40-carat-vs1-clarity-sku-12142391', 'metaTitle': None, 'metaDescription': None, 'targetSite': 'Jamesallen', 'status': {'id': 1, 'name': 'Available'}, 'media': {'thumb': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_1_first_.jpg', 'hearts': None, 'arrows': None, 'gallery': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_3_first_.jpg', 'gallerySet': 'sgmdirect/photoid/19018210/', 'galleryDisplayType': 'diamondGallery', 'stageDisplayType': 'StageBinary', 'segomaPhotoID': 19018210, 'tab': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_2_first_.jpg', 'stage': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_4_first_.jpg', 'stageSet': 'sgmdirect/photoid/19018210/', 'supperZoom': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_0_first_.jpg', 'supperZoomSet': 'sgmdirect/photoid/19018210/',...