如何在网站上查找字符串的一部分并将其保存

问题描述 投票:0回答:1

所以我想为自己制作一个小型instagram机器人。我会手动打开一些不同的instagram用户的标签页(因为我的机器人无法执行此操作),然后搜索“ sc”或“ snapchat”以及之后的内容。然后我想保存它,然后用'send.keys(Keys.Control + Keys.Tab)更改选项卡,然后再次搜索。但是我能找到并弄清楚的是如何登录。您可以在现有选项卡中运行python程序,然后搜索一些字符串。我最了解的知识是C ++,因为我们是在学校学习的。

driver = webdriver.Chrome(executable_path=r"C:/bin/chromedriver")
driver.implicitly_wait(2)
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
driver.find_element_by_name('username').send_keys(username)
driver.find_element_by_name('password').send_keys(password)
#This is just for the login, getting to the website and finding the username
#and password field to  fillout.

这将用于登录,并且用于单击按钮并进入主屏幕

#Here i couldn´t find a method to click the button so i used the key tab to get
#on the button and then click it
driver.find_element_by_name("password").send_keys(u'\ue007')
ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
driver.implicitly_wait(2)
driver.find_element_by_css_selector("body").click()
#at the end i would click an automated instagram popup away
#here i would go to the searchbar and type in the wanted user
instagram = 'userxy'
driver.find_element_by_css_selector("#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input").send_keys(instagram)
driver.implicitly_wait(15)

我找不到一种有效的方法,或者我无法理解,但是如果instagram用户的个人简历名称中包含他们的名字,就会将其保存到文件中。例如,我将输入“ the rock”,如果他的传记中有“ Snapchat:therock”这样的字样,它将保存整个字符串

python selenium web-scraping instagram
1个回答
0
投票

根据您的问题描述,听起来您想在Instagram上搜索并从某人的个人资料中获取结果。我可以为您提供一个有关如何执行此操作的粗略示例:

# locate search bar and send text to it
driver.find_element_by_xpath("//input[@placeholder='Search']").send_keys("userxy")

# click the first search result that pops up
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='fuqBx']/a[1]"))).click()

# get the text from their instagram bio
bio = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='-vDIg']/span"))).text

# check if text contains "snapchat"
if ("snapchat" in bio):

    # split the instagram bio by newline char to get line with snapchat name
    bio_lines = bio.split("\n")

    # parse over the instagram bio to find snapchat username
    for line in bio_lines:

        # if we find the line with username, strip out text to get the username
        if ("Snapchat:" in line):
            snapchat_username = line.replace("Snapchat:", "")

            # you probably need to do something here to save to file
            print(snapchat_username)    

这是非常通用的代码,只是为了帮助您入门。值得注意的是,基本上不可能编写一种简单的方法来100%地找到gmail用户名。每个人对Instagram简历的格式设置都不同,也许他们用“ sc:mysnapchat”而不是“ Snapchat:therock”表示用户名。

希望此代码可以使您入门。

© www.soinside.com 2019 - 2024. All rights reserved.