【问题标题】:Scraping followers from Twitter using Selenium使用 Selenium 从 Twitter 抓取关注者
【发布时间】:2022-01-16 10:54:56
【问题描述】:

我是 Python 新手,一直在尝试编写一个可以从 Twitter 抓取数据的应用程序。我尝试在堆栈和互联网上搜索所有类似的可能解决方案,但失败了。

我想抓取所有这些用户名: See here

这是我的代码:

driver.get("https://twitter.com/twitterusername/followers")
sleep(10)

usernames = driver.find_elements_by_class_name("css-901oao.css-16my406.r-poiln3.rbcqeeo.r-qvutc0")
for username in usernames:
    print(username.get_attribute("href"))

我得到的结果:

None
None
None
None
None
None
None

... 继续

感谢您的帮助。

【问题讨论】:

  • 为什么是巨大的sleep(10)
  • 我只是在测试它。我稍后会改变它。你能帮我么? :(
  • 使用这种类不是一个好习惯,因为它们被混淆了并且会改变。只需使用父类并接触到下面的孩子。 document.querySelectorAll('div[aria-label="Timeline: Followers"] a[role="link"]').forEach(a => console.log(a['href']))
  • 我不明白这个...你能用我的代码为我提供修复吗?
  • 我正在使用 Python。我认为上面的代码是在 Java/JavaScript 中的。

标签: python selenium twitter


【解决方案1】:

所以,使用 BeautifulSoup 是不可能的。 我们只能使用 selenium 来处理这个问题。

for a in driver.find_elements_by_xpath('//div[@aria-label="Timeline: Followers"]//a[@role="link"]'):
    url = a.get_property('href')
    if 'search' in url:
        return 
    print(url.replace("https://twitter.com/", "@")

【讨论】:

  • 感谢您的努力,但我打算在 Selenium 中执行此操作。 :(
  • 使用像 BeautifulSoup 这样的解析器有什么问题?你不能安装额外的灯包?
  • 我正在使用 Selenium 并且我的大部分功能都是在其中完成的...... :( 很抱歉,我不是故意的。我非常感谢您的时间和精力。 .. 只是我更喜欢 Selenium。
  • 不用担心,我更新了我的答案并且只使用了 selenium :)
  • 它有效。有一个问题......它还显示了其他链接。像这样:twitter.com/search?q=%23Whexcosystem&src=hashtag_click
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 2012-07-20
  • 2021-05-22
相关资源
最近更新 更多