Q: How to write a python script to get a json for a speicific comic

Avatar image for moxxwell
Moxxwell

2

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

Hi! I'm very new to API, so please bear with me.

I have the following api reqest to get a comic info by id:

response = requests.get("https://comicvine.gamespot.com/api/issue/4000-14582/?api_key=YOUR-KEY&format=json")

... but only get a 403 as a response. However doing this inside a browser I get a nice json as a response.

Any idea why?

Avatar image for buzzinteractiv1
buzzinteractiv1

1

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

To write a Python script that fetches a JSON for a specific comic and allows you to write a comment, you can use the requests library to make API calls and the json module to handle JSON data. For this example, I'll assume you want to interact with a web API that provides comic data and comment functionality. https://www.buzzinteractive.co/

Here's a short Python script that demonstrates how to achieve this:

python
import requests def get_comic(comic_id): api_url = f"https://api.example.com/comics/{comic_id}" response = requests.get(api_url) if response.status_code == 200: comic_data = response.json() return comic_data else: print(f"Failed to get comic data. Status code: {response.status_code}") return None def post_comment(comic_id, comment): api_url = f"https://api.example.com/comments" comment_data = {"comic_id": comic_id, "comment": comment} response = requests.post(api_url, json=comment_data) if response.status_code == 201: print("Comment posted successfully!") else: print(f"Failed to post comment. Status code: {response.status_code}") def main(): comic_id = "123" # Replace with the specific comic ID you want to retrieve and comment on comment = "This comic is hilarious!" # Replace with your desired comment comic_data = get_comic(comic_id) if comic_data: print(f"Comic Title: {comic_data['title']}") print(f"Comic Description: {comic_data['description']}") post_comment(comic_id, comment) if __name__ == "__main__": main()

Remember to replace the placeholder URLs with the actual API endpoints provided by the comic service you're interacting with. Additionally, make sure you have the requests library installed in your Python environment. You can install it using pip:


pip install requests

Please note that this example assumes the API endpoints and data format for the comic service and comment functionality. Adjust the code accordingly based on the actual API documentation and requirements.