Python requests.post()返回<;答复[406]>;
我试图从一个电子商务网站上获取数据,我找到了按类别显示其产品信息的API。我将这些放在python控制台中:Python requests.post()返回<;答复[406]>;,python,http,python-requests,web-crawler,Python,Http,Python Requests,Web Crawler,我试图从一个电子商务网站上获取数据,我找到了按类别显示其产品信息的API。我将这些放在python控制台中: import requests response = requests.post('https://gql.tokopedia.com/', data={"operationName":"SearchProductQuery","variables":{"params":"&ob=23&
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)
我创建的网站是。仅供参考。您的参数“data”
错误。您需要传递字符串而不是dict。(它不是表单数据
),请尝试下面的代码:
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())
这给了我:
200
[{'data': {'ticker': {'tickers': [{'message': 'Mau belanja aman? <span class="highlights">Masuk yuk!</span>', '__typename': 'TickerData'}], '__typename': 'Tickers'}}}]
200
[{'data':{'ticker':{'tickers':[{'message':'Mau belanja aman?Masuk yuk!'、'uuuuu typename':'TickerData'}]、'uuuu typename':'tickers'}]
我尝试了您的有效负载参数,结果很好,但只要我使用自己的有效负载参数,它仍然会返回406。为什么?@LeonardLee您的有效负载无效。如果您使用正确的格式发布有效负载,它将不会返回406。正确的格式应该与我发布的有效负载相同。但即使我将有效负载更改为字符串格式,它仍会返回406。但我已经转向selenium,这很管用。@LeonardLee将负载从负载“更改为字符串”的格式不正确。显然,它是一个列表,而不是裸体dict。无论如何,它可以在我的电脑上正常工作。