Issue
I am trying to build a bot to scrape some posts from twitter for learning purpose. The issue is the browser gets opened and asks to login. But I have actually logged in priorly. Why is it asking to login again? Is it something missing in my code? Can someone guide me if I am going wrong here. If I dont log in, it will give the empty Dataframe. Please help me.
import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get('https://twitter.com/search?q=python&src=typed_query')
driver.maximize_window()
time.sleep(5)
tweets=driver.find_elements(by='xpath',value='//article[@role="article"]')
user_data=[]
text_data=[]
for tweet in tweets:
user=tweet.find_elements(by='xpath',value='//span[contains(text(),'@')]').text
text=tweet.find_elements(by='xpath',value='//div[@lang]').text
user.append(user_data)
text.append(text_data)
driver.quit()
df_tweets=pd.DataFrame({'user':user_data,'text':text_data})
df_tweets.to_csv('tweets.csv',index=False)
print(df_tweets)
Solution
Afaik, selenium doesn't actually use your actual chrome browser instance. It creates another instance of it. It doesn't matter if you have logged into twitter in chrome. You have to login separately when using selenium.
Answered By - Clumsy66
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.