Interesting API introduction 1.0

Posted by pushpendra.php on Sun, 28 Nov 2021 05:07:54 +0100

1 Preface

Friends, it's a beautiful weekend again. Did you have a good time this week? Have you been praised? Have you figured out what to do for the weekend? Today, let's start with these questions and talk about the romance and sand sculpture happiness brought by data science.

2 two sand carving and interesting API s

Maybe after working / studying for a week, you are very tired now; Maybe it's winter, cold and going to work makes people unhappy; Maybe I've been sitting for a long time and eating too much. After all, every day is cooking day. The sore shoulders and heavy fat make people tired. So let's introduce the first sand sculpture API to you, and you can automatically generate the local flavor to make you happy. If you don't say much, you'll get the result first.

Examples of local love stories

Think about what to do every weekend. I don't know if you have the same trouble. If so, the following border API may help you. Its API is like its name. Whenever you are bored, it can randomly generate some activities for your reference. Including the content, type, number of participants, price, link (if any) and feasibility of the event. For example, I randomly generated it in the morning, and now I'm doing the fifth thing it recommends to me:)

Activity recommendation example

Of course, you can modify it as you like. The following is a reference to the activity parameters of the boredAPI. The official of the boredAPI is also attached here Reference documents . You can limit the type, number of participants, price, feasibility, etc. you like to make it more meet your needs. For example, one day I worked hard and only looked at the activities of education class. An example is as follows. We have surprisingly found that learning activities are really almost zero cost, ready to start at any time, and one person is feasible. I can't find a reason not to study anymore

Examples of learning activities

3 code implementation

API(Application Programming Interface) is an application programming interface. Let's not repeat the wheel, we can use only a few lines of code to achieve a variety of interesting functions. In the following demo, we call two API to generate random love speech and activity recommendation. All you have to do is run it again. The code is as follows:

import requests
import json
import random
import time
import pandas as pd

user_agents = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60',
    'Opera/8.0 (Windows NT 5.1; U; en)',
    'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
    'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 ',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)"
]

def getHeaders():
    user_agent = user_agents[random.randint(0, len(user_agents)-1)] 
    headers = {
        'User-Agent': user_agent
    }
    return headers

#  Tired, listen to today's Rainbow fart
sweet_words = []
url1 = 'https://chp.shadiao.app/api.php'
for i in range(5):
    time.sleep(random.uniform(0.1,1))
    response1 = requests.get(url1, headers=getHeaders())
    sweet_words.append(response1.text)
truth = pd.DataFrame(sweet_words,columns=['Don't look. It's you'])
truth


#  Let's see what we can do at the weekend
todo = []
#Randomly generated activity
url2 = 'http://www.boredapi.com/api/activity/'
#Randomly generate activities of the specified type. The following example is education.
# url2 = 'http://www.boredapi.com/api/activity?type=education'
for i in range(5):
    time.sleep(random.uniform(0.1,1))
    response2 = requests.get(url2, headers=getHeaders())
    item = json.loads(response2.text)
    todo.append((item["activity"],item["type"],item["participants"],item["price"],item["link"],item["accessibility"]))
todo_list = pd.DataFrame(todo,columns=['matter','category','Number of participants','Price(0-1)','link','feasibility(0-1)'])
todo_list['feasibility(0-1)'] = 1-todo_list['feasibility(0-1)'] 
todo_list

4 Summary

The two APIs introduced today are very easy to use because they do not require a network token or API key, and do not have the trouble of privacy disclosure and advertising during registration. As long as you have a Python environment and run code without brain, you can get different information every time. I hope it can bring you a little smile and joy. So, have a nice weekend!

Topics: Python api Data Analysis