import requests
from bs4 import BeautifulSoup
List of Twitter accounts to scrape
twitter_accounts = [
“https://twitter.com/Mr_Derivatives”,
“https://twitter.com/warrior_0719”,
]
def scrape_twitter_accounts(accounts):
headers = {
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3”
}
for account in accounts:
try:
# Fetch HTML content of the Twitter account with custom headers
session = requests.Session()
response = session.get(account, headers=headers)
# response = requests.get(account, headers=headers)
print(f"1")
response.raise_for_status() # Raise an exception for unsuccessful requests
print(f"-1")
# Parse HTML content using BeautifulSoup with 'lxml' parser
soup = BeautifulSoup(response.text, 'html.parser')
# Find all tweets on the page
tweets = soup.find_all('div', class_='js-tweet-text-container')
for tweet in tweets:
# Extract tweet text
tweet_text = tweet.text.strip()
print(tweet_text)
except Exception as e:
print(f"Error fetching {account}: {e}")
Call the function with the list of Twitter accounts
scrape_twitter_accounts(twitter_accounts)