News website using Flask and news API - working with API : Part 4
Hello guys,
Today we will learn about working with API using python.
Get Started:
First Step is to get API key
For that, just login to NewsAPI and get the unique key.
That's it for today.
Next Working with templates
Today we will learn about working with API using python.
What is API?
An application programming interface is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.
what is NewsAPI ?
News API is a simple HTTP REST API for searching and retrieving live articles from all over the web. It can help you answer questions like:
- What top stories is TechCrunch running right now?
- What new articles were published about the next iPhone today?
- Has my company or product been mentioned or reviewed by any blogs recently?
First Step is to get API key
For that, just login to NewsAPI and get the unique key.
Using API with Python:
We need request module or package.
If you have learned java before you can term this as a package with number of functions and classes.
Requests:
Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your
PUT
& POST
data — but nowadays, just use the json
method!
To install package
pip install requests
app.py
from flask import Flask
import requests as rq
# intialise data of lists.
app = Flask(__name__)
@app.route('/')
def index():
api_key = "Your API key"
url = "http://newsapi.org/v2/top-headlines?country=in&apiKey=" + api_key
data = rq.get(url)
print(data) #check if request getting response or not
data = data.json()
print(data)
articles = data["articles"]
return "Hello World"
if __name__ == "__main__":
app.run(debug=True)
Most of the code is the same as the previous hello world.
- api_key - Key that you get from NewsAPI (This key is completely unique)
- url - Simple URL with parameters
- apiKey. Every big organization wants to track data like how many requests are generated by a specific user for that purpose they give a unique key to their API.
- Country - You can assign any country if you want ( Country Code ) - 54 countries are supported
- get() - This method request for specified URL and get a response
- data.json() - This will return data in JSON format
- data["articles"] - Return list of articles from data
Output:
(env) E:\flask\newsapp>py app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 100-183-505
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
<Response [200]>
{
- "status": "ok",
- "totalResults": 38,
- -"articles": [
- -{
- -"source": {
- "id": "the-times-of-india",
- "name": "The Times of India"
- "author": "AFP",
- "title": "China parliament adopts plan to impose security law on Hong Kong - Times of India",
- "description": "China News: Over 2,800 members of the National People's Congress (NPC) voted overwhelmingly in favour of the proposal to draft the law, which would punish secessi",
- "url": "https://timesofindia.indiatimes.com/world/china/china-parliament-adopts-plan-to-impose-security-law-on-hong-kong/articleshow/76063833.cms",
- "urlToImage": "https://static.toiimg.com/thumb/msid-76063834,width-1070,height-580,imgsize-126911,resizemode-75,overlay-toi_sw,pt-32,y_pad-40/photo.jpg",
- "publishedAt": "2020-05-28T10:40:21Z",
- "content": null
- ...]
- }
127.0.0.1 - - [28/May/2020 17:46:23] "?[37mGET / HTTP/1.1?[0m" 200 -
That's it for today.
Next Working with templates
❤❤Quarantine python group link ❤❤
8805271377 WhatsApp
Follow here ❤
@mr._mephisto_ Instagram
There will be no restrictions just feel free to learn.
Share and take one more step to share knowledge with others.
Believe in yourself 🤟 you are awesome.
Be safe, Be happy😁
Take care of yourself and your family
Of course, watch movies and series🤟😉
And follow the guidelines of the government
Comments
Post a Comment