【问题标题】:Gather usernames in instagram在 Instagram 中收集用户名
【发布时间】:2021-03-07 21:26:04
【问题描述】:

主要问题是我想收集并获取喜欢我的帖子的用户名,谁看过我的故事......

当我拥有这些用户名的列表时,我可以了解我的追随者中谁是幽灵,谁是忠诚于我的人......

我用 selenium 写了一些东西,它会登录到 Instagram。但我不能,我不知道如何在其中保存任何用户名。有什么想法吗?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
browser = webdriver.Chrome(options=chrome_options)

browser.get('https://www.instagram.com/p/CMAVjR5CmOx/')

c2 = browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[2]/div/div[2]/a")
c2.click()

c3 = browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/div/div/div[1]/div[2]/div[2]/div").text
print(c3)

【问题讨论】:

    标签: python selenium selenium-webdriver instagram


    【解决方案1】:

    这是登录代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.keys import Keys
    import time
    
    chrome_options = Options()
    chrome_options.add_argument("--user-data-dir=chrome-data")
    browser = webdriver.Chrome(options=chrome_options)
    
    browser.get('https://www.instagram.com/p/CMAVjR5CmOx/')
    time.sleep(3)
    browser.find_element_by_class_name("sqdOP.L3NKy.y3zKF").click()
    time.sleep(3)
    browser.find_element_by_name("username").send_keys("username")
    browser.find_element_by_name("password").send_keys("password"+Keys.RETURN)
    

    【讨论】:

    • 我知道如何登录,问题是我想在登录后获取用户名
    【解决方案2】:

    对于初学者,您需要知道要定位哪些信息(元素)。 然后你需要编写更好的 xpath。你可以在这里了解更多:https://www.w3schools.com/xml/xpath_intro.asp

    您希望它们简洁且不像您指定的那样脆弱。 像这样点击点赞按钮:

    //a[contains(@href,'liked_by')]
    

    然后这个来抓取所有的对话框容器:

    //div[@role='dialog']
    

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2018-03-16
      相关资源
      最近更新 更多