简体   繁体   中英

Python requests.post() returns <Response [406]>

I was trying to get the data from an eCommerce website, and I found the API to their products information by category. I put these in my python console:

import requests


response = requests.post('https://gql.tokopedia.com/', data={"operationName":"SearchProductQuery","variables":{"params":"&ob=23&identifier=dapur_aksesoris-dapur_alat-pemotong-serbaguna&sc=3470&user_id=110487876&rows=60&start=1&source=directory&device=desktop&page=1&related=true&st=product&safe_search=false","adParams":"&page=1&dep_id=3470&ob=23&ep=product&item=15&src=directory&device=desktop&user_id=110487876&minimum_item=15&start=1&no_autofill_range=5-14"},"query":"query SearchProductQuery($params: String, $adParams: String) {\n  CategoryProducts: searchProduct(params: $params) {\n    count\n    data: products {\n      id\n      url\n      imageUrl: image_url\n      imageUrlLarge: image_url_700\n      catId: category_id\n      gaKey: ga_key\n      countReview: count_review\n      discountPercentage: discount_percentage\n      preorder: is_preorder\n      name\n      price\n      original_price\n      rating\n      wishlist\n      labels {\n        title\n        color\n        __typename\n      }\n      badges {\n        imageUrl: image_url\n        show\n        __typename\n      }\n      shop {\n        id\n        url\n        name\n        goldmerchant: is_power_badge\n        official: is_official\n        reputation\n        clover\n        location\n        __typename\n      }\n      labelGroups: label_groups {\n        position\n        title\n        type\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n  displayAdsV3(displayParams: $adParams) {\n    data {\n      id\n      ad_ref_key\n      redirect\n      sticker_id\n      sticker_image\n      productWishListUrl: product_wishlist_url\n      clickTrackUrl: product_click_url\n      shop_click_url\n      product {\n        id\n        name\n        wishlist\n        image {\n          imageUrl: s_ecs\n          trackerImageUrl: s_url\n          __typename\n        }\n        url: uri\n        relative_uri\n        price: price_format\n        campaign {\n          original_price\n          discountPercentage: discount_percentage\n          __typename\n        }\n        wholeSalePrice: wholesale_price {\n          quantityMin: quantity_min_format\n          quantityMax: quantity_max_format\n          price: price_format\n          __typename\n        }\n        count_talk_format\n        countReview: count_review_format\n        category {\n          id\n          __typename\n        }\n        preorder: product_preorder\n        product_wholesale\n        free_return\n        isNewProduct: product_new_label\n        cashback: product_cashback_rate\n        rating: product_rating\n        top_label\n        bottomLabel: bottom_label\n        __typename\n      }\n      shop {\n        image_product {\n          image_url\n          __typename\n        }\n        id\n        name\n        domain\n        location\n        city\n        tagline\n        goldmerchant: gold_shop\n        gold_shop_badge\n        official: shop_is_official\n        lucky_shop\n        uri\n        owner_id\n        is_owner\n        badges {\n          title\n          image_url\n          show\n          __typename\n        }\n        __typename\n      }\n      applinks\n      __typename\n    }\n    template {\n      isAd: is_ad\n      __typename\n    }\n    __typename\n  }\n}\n"})
print(response)

The website I started from is https://www.tokopedia.com/p/dapur/aksesoris-dapur/alat-pemotong-serbaguna . For your information.

Your argument "data" is wrong.You need to pass a string instead of dict.(It is not form-data ),Try code below:

import requests

payload = "[{\"operationName\":\"Ticker\",\"variables\":{\"page\":\"header\"},\"query\":\"query Ticker($page: String) {\\n  ticker {\\n    tickers(page: $page) {\\n      message\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n\"}]"

url = 'https://gql.tokopedia.com/'

with requests.Session() as s:
    print(response.status_code)
    print(response.json())

This gave me:

200
[{'data': {'ticker': {'tickers': [{'message': 'Mau belanja aman? <span class="highlights">Masuk yuk!</span>', '__typename': 'TickerData'}], '__typename': 'Tickers'}}}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM