Example output
Here is a code snippet for connecting to Twitter API using tweepy library and retrieving user timeline data:
python
Copy code
import tweepy
# Set up credentials
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
# Authenticate with Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Initialize Tweepy API object
api = tweepy.API(auth)
# Retrieve user timeline data
tweets = api.user_timeline(screen_name='twitter_username', count=10)
# Print each tweet's text
for tweet in tweets:
print(tweet.text)
Here is a code snippet for displaying the retrieved Twitter timeline data in a website written in CSS:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Twitter Timeline</title>
<style>
/* Add CSS styles here */
</style>
</head>
<body>
<div id="tweets">
<!-- Populate tweets dynamically using JavaScript -->
</div>
<script>
// Retrieve tweets using JavaScript
fetch('/api/tweets') // Assumes Flask or other web framework is used
.then(response => response.json())
.then(data => {
// Create HTML elements for each tweet
data.forEach(tweet => {
const tweetDiv = document.createElement('div');
tweetDiv.classList.add('tweet');
const tweetText = document.createElement('p');
tweetText.innerText = tweet.text;
tweetDiv.appendChild(tweetText);
const tweetDate = document.createElement('p');
tweetDate.innerText = tweet.created_at;
tweetDiv.appendChild(tweetDate);
// Add tweet to the tweets container
document.getElementById('tweets').appendChild(tweetDiv);
});
})
.catch(error => console.error(error));
</script>
</body>
</html>
To display the tweets in the website, you would need to set up a web server that runs the Python code and serves the HTML file. You can use a web framework like Flask to make this easier.
To save the first code snippet, you can save it in a file named "twitter_api.py". To save the second code snippet, you can save it in a file named "twitter_timeline.html".