我是这个平台的新手,如果我询问的方式有误,我很抱歉。 我想使用 selenium 从 twitter(X) 获取数据,但有问题,我不明白。 我想使用边缘驱动程序,因为我遇到了谷歌驱动程序版本错误
运行代码时得到的输出AttributeError:'SeleniumClient'对象没有属性'Edge_options'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd
class SeleniumClient(object):
def __init__(self):
#Initialization method.
self.Edge = webdriver.EdgeOptions()
self.Edge_options('--headless')
self.Edge_options.add_argument('--no-sandbox')
self.Edge_options.add_argument('--disable-setuid-sandbox')
# you need to provide the path of chromdriver in your system
self.browser = webdriver.Edge(executable_path=r'C:\Users\Trinity\edgedriver\msedgedriver.exe', options=self.Edge_options)
self.base_url = 'https://twitter.com/search?q='
def get_tweets(self, query):
'''
Function to fetch tweets.
'''
try:
self.browser.get(self.base_url+query)
time.sleep(2)
body = self.browser.find_element_by_tag_name('body')
for _ in range(3000):
body.send_keys(Keys.PAGE_DOWN)
time.sleep(0.3)
timeline = self.browser.find_element_by_id('timeline')
tweet_nodes = timeline.find_elements_by_css_selector('.tweet-text')
return pd.DataFrame({'tweets': [tweet_node.text for tweet_node in tweet_nodes]})
except:
print("Selenium - An error occured while fetching tweets.")
selenium_client = SeleniumClient()
tweets_df = selenium_client.get_tweets('AI and Deep learning'
当我运行代码时,我以为我可以从 Twitter 获取数据(推文抓取)
Selenium 期望按以下方式设置选项。这仅适用于 Selenium 4 及以上版本。
EdgeOptions options = new EdgeOptions();
driver = new EdgeDriver(options);
有关更多详细信息,请参阅:https://www.selenium.dev/documentation/webdriver/browsers/edge/