Stack Exchange API#
By Sebastian Shirk
Stack Exchange API: https://api.stackexchange.com/docs
API Terms of Use: https://stackoverflow.com/legal/api-terms-of-use
Stack Overflow Public Network Tems of Service: https://stackoverflow.com/legal/terms-of-service/public#licensing
These recipe examples were tested on August 27, 2024.
Import Libraries#
import requests
import json
import datetime
import pandas as pd
BASE_URL = "https://api.stackexchange.com/2.3/"
2. Get Questions Based on Title#
Change the title
variable to get questions based on different titles.
title = 'How to use Python'
def search_questions(query, site='stackoverflow'):
url = f"{BASE_URL}search?order=desc&sort=activity&intitle={query}&site={site}"
response = requests.get(url)
return response.json()
print("\nSearching for questions with 'How to use Python' in the title...")
search_results = search_questions(title)
if search_results['items']:
search_result = search_results['items'][0]
search_result['last_activity_date'] = convert_unix_to_readable(search_result['last_activity_date'])
search_result['creation_date'] = convert_unix_to_readable(search_result['creation_date'])
print(json.dumps(search_result, indent=2))
# To print specific fields use this format:
print(f"Title: {question['title']}")
print(f"Link: {question['link']}")
print(f"Creation Date: {question['creation_date']}")
else:
print("No questions found.")
Searching for questions with 'How to use Python' in the title...
{
"tags": [
"python",
"multiprocessing",
"queue"
],
"owner": {
"account_id": 28371352,
"reputation": 42,
"user_id": 21699895,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/SCmE0.jpg?s=256",
"display_name": "kartiks77",
"link": "https://stackoverflow.com/users/21699895/kartiks77"
},
"is_answered": false,
"view_count": 31,
"answer_count": 1,
"score": -1,
"last_activity_date": "2024-08-22 04:42:59",
"creation_date": "2024-08-21 08:41:16",
"question_id": 78897306,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/78897306/how-to-use-python-multiprocessing-with-undefined-queue-task-done",
"title": "How to use Python Multiprocessing with Undefined Queue.task_done()"
}
Title: How do you manage shared python libraries across applications/scripts on the same server? How do you handle imports and how to manage version control?
Link: https://stackoverflow.com/questions/78920087/how-do-you-manage-shared-python-libraries-across-applications-scripts-on-the-sam
Creation Date: 2024-08-27 12:19:08
3. Get Questions Based on Similarity#
Change the title
variable to get questions based on different titles.
title = 'How to use Python'
def get_similar_questions(title, site='stackoverflow'):
url = f"{BASE_URL}similar?order=desc&sort=activity&title={title}&site={site}"
response = requests.get(url)
return response.json()
def convert_unix_to_readable(unix_time):
return datetime.datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d %H:%M:%S')
def get_questions_data(tag, site='stackoverflow'):
url = f"{BASE_URL}questions?order=desc&sort=activity&tagged={tag}&site={site}"
response = requests.get(url)
return response.json()
print("\nGetting similar questions to 'How to use Python for web scraping'...")
similar_questions = get_similar_questions(title)
if similar_questions['items']:
similar_question = similar_questions['items'][0]
similar_question['last_activity_date'] = convert_unix_to_readable(similar_question['last_activity_date'])
similar_question['creation_date'] = convert_unix_to_readable(similar_question['creation_date'])
print(json.dumps(similar_question, indent=2))
# To print specific fields, use this format:
print(f"Title: {question['title']}")
print(f"Link: {question['link']}")
print(f"Creation Date: {question['creation_date']}")
else:
print("No similar questions found.")
Getting similar questions to 'How to use Python for web scraping'...
{
"tags": [
"php",
"routes",
"yaml",
"typo3",
"extbase"
],
"owner": {
"account_id": 10762457,
"reputation": 1,
"user_id": 7918472,
"user_type": "registered",
"profile_image": "https://www.gravatar.com/avatar/7063ee90d66ead28d2b589e16eb85434?s=256&d=identicon&r=PG",
"display_name": "C. Widmer",
"link": "https://stackoverflow.com/users/7918472/c-widmer"
},
"is_answered": false,
"view_count": 48,
"answer_count": 1,
"score": 0,
"last_activity_date": "2024-08-27 16:01:29",
"creation_date": "2024-06-06 02:21:16",
"question_id": 78585023,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/78585023/routing-of-own-extension-fails-for-detail-page",
"title": "Routing of own extension fails for detail-page"
}
Title: How do you manage shared python libraries across applications/scripts on the same server? How do you handle imports and how to manage version control?
Link: https://stackoverflow.com/questions/78920087/how-do-you-manage-shared-python-libraries-across-applications-scripts-on-the-sam
Creation Date: 2024-08-27 12:19:08
4. Get Answers and Comments to a Question#
Get all answers to a question based on the question ID. Question ID can be found using the above methods.
Comments appear above the answers in the output.
A filter is used on this query. Filters can be found here: https://api.stackexchange.com/docs/answers-on-questions#order=desc&sort=activity&ids=33180743&filter=!22ZfkjBmMx*-LZeN4vanL&site=stackoverflow&run=true
Use the filter ‘[edit]’ on the ‘Try It’ section of the page to get the filter string if you want to use a different filter.
# Get all answers to a specific question using a uestion id
question_id = 33180743
filter = '!22ZfkjBmMx*-LZeN4vanL'
def get_answers_to_question(question_id, site='stackoverflow'):
# Filters found here: https://api.stackexchange.com/docs/answers-on-questions#order=desc&sort=activity&ids=33180743&filter=!22ZfkjBmMx*-LZeN4vanL&site=stackoverflow&run=true
# Use the filter '[edit]' on the 'Try It' section of the page to get the filter string
url = f"{BASE_URL}questions/{question_id}/answers?order=desc&sort=activity&site={site}&filter={filter}"
response = requests.get(url)
return response.json()
print("\nGetting answers to a specific question...")
answers_data = get_answers_to_question(question_id)
print(json.dumps(answers_data, indent=2))
Getting answers to a specific question...
{
"items": [
{
"owner": {
"account_id": 5704861,
"reputation": 5383,
"user_id": 4508758,
"user_type": "registered",
"accept_rate": 71,
"profile_image": "https://i.sstatic.net/GsmdpxPQ.jpg?s=256",
"display_name": "Rodrigo João Bertotti",
"link": "https://stackoverflow.com/users/4508758/rodrigo-jo%c3%a3o-bertotti"
},
"comment_count": 0,
"is_accepted": false,
"score": 0,
"last_activity_date": 1723064853,
"creation_date": 1723064853,
"answer_id": 78845678,
"question_id": 33180743,
"content_license": "CC BY-SA 4.0",
"share_link": "https://stackoverflow.com/a/78845678",
"body": "<p>For someone testing on Emulator, try choosing an emulator that supports Google Play, then Sign In on Google Play</p>\n"
},
{
"comments": [
{
"owner": {
"account_id": 9675105,
"reputation": 881,
"user_id": 7178860,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/77XfH.jpg?s=256",
"display_name": "Mohammad",
"link": "https://stackoverflow.com/users/7178860/mohammad"
},
"edited": false,
"score": 0,
"creation_date": 1670227539,
"post_id": 44616079,
"comment_id": 131818383,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/44616079#comment131818383_44616079",
"body": "Should I log in with a google account that has payment method enabled? Just having play service isn't enough?"
},
{
"owner": {
"account_id": 997107,
"reputation": 2171,
"user_id": 1012786,
"user_type": "registered",
"profile_image": "https://www.gravatar.com/avatar/cf2bb4340d5772402315f858090fc88c?s=256&d=identicon&r=PG",
"display_name": "jaybers",
"link": "https://stackoverflow.com/users/1012786/jaybers"
},
"reply_to_user": {
"account_id": 9675105,
"reputation": 881,
"user_id": 7178860,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/77XfH.jpg?s=256",
"display_name": "Mohammad",
"link": "https://stackoverflow.com/users/7178860/mohammad"
},
"edited": false,
"score": 0,
"creation_date": 1670263871,
"post_id": 44616079,
"comment_id": 131831230,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/44616079#comment131831230_44616079",
"body": "Yes - The user has to be logged in to the play store with the ability to purchase."
},
{
"owner": {
"account_id": 9675105,
"reputation": 881,
"user_id": 7178860,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/77XfH.jpg?s=256",
"display_name": "Mohammad",
"link": "https://stackoverflow.com/users/7178860/mohammad"
},
"edited": false,
"score": 0,
"creation_date": 1670265152,
"post_id": 44616079,
"comment_id": 131831592,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/44616079#comment131831592_44616079",
"body": "Hmm, seems like I can never do that due to where I'm born and live :)"
},
{
"owner": {
"account_id": 997107,
"reputation": 2171,
"user_id": 1012786,
"user_type": "registered",
"profile_image": "https://www.gravatar.com/avatar/cf2bb4340d5772402315f858090fc88c?s=256&d=identicon&r=PG",
"display_name": "jaybers",
"link": "https://stackoverflow.com/users/1012786/jaybers"
},
"reply_to_user": {
"account_id": 9675105,
"reputation": 881,
"user_id": 7178860,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/77XfH.jpg?s=256",
"display_name": "Mohammad",
"link": "https://stackoverflow.com/users/7178860/mohammad"
},
"edited": false,
"score": 1,
"creation_date": 1670286923,
"post_id": 44616079,
"comment_id": 131836912,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/44616079#comment131836912_44616079",
"body": "I don't follow your use case but ..... you can always handle the error gracefully and tell the user that in app purchases are not supported."
},
{
"owner": {
"account_id": 18144463,
"reputation": 330,
"user_id": 13197940,
"user_type": "registered",
"profile_image": "https://i.sstatic.net/jxnpt.jpg?s=256",
"display_name": "Daniel",
"link": "https://stackoverflow.com/users/13197940/daniel"
},
"edited": false,
"score": 0,
"creation_date": 1679670374,
"post_id": 44616079,
"comment_id": 133767950,
"content_license": "CC BY-SA 4.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/44616079#comment133767950_44616079",
"body": "How can I check if the user is logged into google play?"
}
],
"owner": {
"account_id": 997107,
"reputation": 2171,
"user_id": 1012786,
"user_type": "registered",
"profile_image": "https://www.gravatar.com/avatar/cf2bb4340d5772402315f858090fc88c?s=256&d=identicon&r=PG",
"display_name": "jaybers",
"link": "https://stackoverflow.com/users/1012786/jaybers"
},
"comment_count": 5,
"is_accepted": false,
"score": 60,
"last_activity_date": 1681487831,
"last_edit_date": 1681487831,
"creation_date": 1497796585,
"answer_id": 44616079,
"question_id": 33180743,
"content_license": "CC BY-SA 4.0",
"share_link": "https://stackoverflow.com/a/44616079",
"body": "<p>I get In-app billing version 3 NOT supported error when the user is not signed into google play. Ensure a user is logged into google play on the device.</p>\n<p>Update 2023: Note that you might also get the error "Google Play In-app Billing API version is less than 3" when the user is not logged in to the play store.</p>\n"
},
{
"comments": [
{
"owner": {
"account_id": 2410923,
"reputation": 2387,
"user_id": 2105988,
"user_type": "registered",
"accept_rate": 79,
"profile_image": "https://i.sstatic.net/FBQMq.jpg?s=256",
"display_name": "rareyesdev",
"link": "https://stackoverflow.com/users/2105988/rareyesdev"
},
"edited": false,
"score": 2,
"creation_date": 1445883599,
"post_id": 33352575,
"comment_id": 54501333,
"content_license": "CC BY-SA 3.0",
"link": "https://stackoverflow.com/questions/33180743/in-app-billing-version-3-not-supported-with-up-to-date-play-store/33352575#comment54501333_33352575",
"body": "Not working. I cleared data on both apps and stopped Google Play."
}
],
"owner": {
"account_id": 466110,
"reputation": 24941,
"user_id": 870674,
"user_type": "registered",
"accept_rate": 80,
"profile_image": "https://i.sstatic.net/WWmSK.jpg?s=256",
"display_name": "klimat",
"link": "https://stackoverflow.com/users/870674/klimat"
},
"comment_count": 1,
"is_accepted": false,
"score": 4,
"last_activity_date": 1446456453,
"last_edit_date": 1446456453,
"creation_date": 1445882505,
"answer_id": 33352575,
"question_id": 33180743,
"content_license": "CC BY-SA 3.0",
"share_link": "https://stackoverflow.com/a/33352575",
"body": "<p>Try \"Clear Data\" and then \"Force stop\" for Google Play app.</p>\n"
}
],
"has_more": false,
"quota_max": 300,
"quota_remaining": 296
}