Python script that will get a search term from the user and search for related articles on Medium…

1 year ago 95
BOOK THIS SPACE FOR AD
ARTICLE AD
import requests

# Replace YOUR_MEDIUM_API_KEY with your own Medium API key
MEDIUM_API_KEY = 'YOUR_MEDIUM_API_KEY'

def search(query):
# Make a request to the Medium API
url = f'https://api.medium.com/v1/search?q={query}'
headers = {'Authorization': f'Bearer {MEDIUM_API_KEY}'}
response = requests.get(url, headers=headers)

# Parse the response
data = response.json()

# Extract the search results
results = data['data']

# Print the search results
for result in results:
title = result['title']['text']
url = result['url']
print(f'{title}: {url}')

# Get the search term from the user
query = input('Enter a search term: ')

# Search for articles on Medium
search(query)

To use this script, you will need to install the requests library:

pip install requests

You will also need to replace YOUR_MEDIUM_API_KEY with your own Medium API key. You can obtain a Medium API key by following the instructions here.

This script works by making a request to the Medium API with the search term specified by the user. It then parses the response and extracts the search results. Finally, it prints the titles and URLs of the search results.

You can modify this script to do more with the search results, such as opening the articles in a web browser or saving them to a file.

Read Entire Article