How to use requests.post method in Zyte API?

Posted 12 months ago by Mohit Aswani

Post a topic
Un Answered
M
Mohit Aswani

Hey all, I wish to scrape data from Instagram posts using Zyte API. Refer below Python code snippet. 

import requests

url = "https://www.instagram.com/api/graphql"

payload = 'lsd=AVrpAQdZw08&variables=%7B%22shortcode%22%3A%22CyOgrc-M2Zf%22%2C%22fetch_comment_count%22%3A40%2C%22fetch_related_profile_media_count%22%3A3%2C%22parent_comment_count%22%3A24%2C%22child_comment_count%22%3A3%2C%22fetch_like_count%22%3A10%2C%22fetch_tagged_user_count%22%3Anull%2C%22fetch_preview_comment_count%22%3A2%2C%22has_threaded_comments%22%3Atrue%2C%22hoisted_comment_id%22%3Anull%2C%22hoisted_reply_id%22%3Anull%7D&doc_id=10015901848480474'

headers = {
  'authority': 'www.instagram.com',
  'accept-language': 'en-GB,en;q=0.9',
  'content-type': 'application/x-www-form-urlencoded',
  'origin': 'https://www.instagram.com',
  'referer': 'https://www.instagram.com',
  'sec-ch-ua': '"Brave";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
  'sec-ch-ua-platform': '"Windows"',
  'sec-ch-ua-platform-version': '"15.0.0"',
  'sec-fetch-site': 'same-origin',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
  'x-fb-lsd': 'AVrpAQdZw08'
}

response = requests.post(url, headers=headers, data=payload)
print(response.text)

 

Since I use below code snippet to extract, how do I get it apply requests.post method and alongside pass customized headers and payload?

import requests
from base64 import b64decode

url = 'https://www.instagram.com/p/C0Ytd2lthFj/'
api_response = requests.post("https://api.zyte.com/v1/extract", auth=("298370123498570", ""),
    json={"url": url, "httpResponseBody": True,},)

response = b64decode(api_response.json()["httpResponseBody"]).decode()

 

Thank you in advance for your assistance.

1 Votes


0 Comments

Login to post a comment