Skip to content

Commit 8462dc7

Browse files
committed
Data Scraping with API
1 parent 0ee50ad commit 8462dc7

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

DataScraping/Data_Scraping.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import tweepy
2+
import requests
3+
4+
# Function to get tweets from Twitter API
5+
def get_tweets(api_key, api_secret_key, access_token, access_token_secret, username):
6+
auth = tweepy.OAuthHandler(api_key, api_secret_key)
7+
auth.set_access_token(access_token, access_token_secret)
8+
api = tweepy.API(auth)
9+
10+
tweets = []
11+
try:
12+
for tweet in tweepy.Cursor(api.user_timeline, screen_name=username, tweet_mode="extended").items(10):
13+
tweets.append(tweet.full_text)
14+
except tweepy.TweepError as e:
15+
print(f"Error: {e}")
16+
17+
return tweets
18+
19+
# Function to get weather data from OpenWeatherMap API
20+
def get_weather(api_key, city):
21+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
22+
response = requests.get(url)
23+
data = response.json()
24+
25+
if response.status_code == 200:
26+
return data
27+
else:
28+
print(f"Error: {data['message']}")
29+
return None
30+
31+
# Function to get stock data from Alpha Vantage API
32+
def get_stock_data(api_key, symbol):
33+
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={api_key}"
34+
response = requests.get(url)
35+
data = response.json()
36+
37+
if "Time Series (Daily)" in data:
38+
return data["Time Series (Daily)"]
39+
else:
40+
print(f"Error: {data['Note']}")
41+
return None
42+
43+
if __name__ == "__main__":
44+
# Replace with your Twitter API credentials
45+
twitter_api_key = "YOUR_TWITTER_API_KEY"
46+
twitter_api_secret_key = "YOUR_TWITTER_API_SECRET_KEY"
47+
twitter_access_token = "YOUR_TWITTER_ACCESS_TOKEN"
48+
twitter_access_token_secret = "YOUR_TWITTER_ACCESS_TOKEN_SECRET"
49+
twitter_username = "target_username"
50+
51+
# Replace with your OpenWeatherMap API key
52+
weather_api_key = "YOUR_OPENWEATHERMAP_API_KEY"
53+
city_name = "New York"
54+
55+
# Replace with your Alpha Vantage API key
56+
alpha_vantage_api_key = "YOUR_ALPHA_VANTAGE_API_KEY"
57+
stock_symbol = "AAPL"
58+
59+
# Retrieve data from APIs
60+
tweets = get_tweets(twitter_api_key, twitter_api_secret_key, twitter_access_token, twitter_access_token_secret, twitter_username)
61+
weather_data = get_weather(weather_api_key, city_name)
62+
stock_data = get_stock_data(alpha_vantage_api_key, stock_symbol)
63+
64+
# Print the results
65+
print("Twitter Tweets:")
66+
for idx, tweet in enumerate(tweets, start=1):
67+
print(f"{idx}. {tweet}")
68+
69+
print("\nWeather Data:")
70+
print(weather_data)
71+
72+
print("\nStock Data:")
73+
print(stock_data)

DataScraping/Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Data Retrieval with APIs
2+
3+
A Python script that retrieves data from various platforms such as social media, weather services, and financial data providers using APIs.
4+
5+
6+
## Introduction
7+
8+
Data Retrieval with APIs is a Python script that demonstrates how to use APIs to fetch data from different platforms. It includes examples of retrieving tweets from Twitter, weather data from OpenWeatherMap, and stock data from Alpha Vantage.
9+
10+

SCRIPTS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,5 @@
118118
| 109\. | Fetch Contributions | This script is a Python tool that fetches pull requests made by a user in a GitHub organization. | [Take Me](./Fetch_Contributions/) | [Sabhi Sharma](https//github.com/sabhisharma-ise)
119119
| 109\. | Domain Name Availability Checker | This script is a Python tool that allows you to check the availability of domain names using the GoDaddy API. | [Take Me](./Domain_Name_Availability/) | [Sabhi Sharma](https//github.com/sabhisharma-ise)
120120
| 110\. | Automatic Spelling Checker and Corrector | This Script is used to detect spelling errors in a text and correct them if the user wishes to do so. | [Take Me](./Automatic_Spelling_Checker_Corrector/) | [Sabhi Sharma](https//github.com/sabhisharma-ise)
121-
| 111\. | File Searcher | The File Search script is a Python tool that allows you to search for files with a specific extension in a directory. It recursively searches through all subdirectories of the specified directory and returns a list of files that match the provided file extension. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/File\Search) | [Srujana Vanka](https://github.com/srujana-16)
121+
| 111\. | File Searcher | The File Search script is a Python tool that allows you to search for files with a specific extension in a directory. It recursively searches through all subdirectories of the specified directory and returns a list of files that match the provided file extension. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/File\Search) | [Srujana Vanka](https://github.com/srujana-16)
122+
| 112\. | Data Scraping | A Python script that retrieves data from various platforms such as social media, weather services, and financial data providers using APIs. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/DataScraping) | [Shraddha Singh](https://github.com/shraddha761)

0 commit comments

Comments
 (0)