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

所以我想为自己做一个小型的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用户的个人简历名称中包含snapchat的名字,则会将其保存到文件中。例如,我将输入“ the rock”,如果他的传记中有“ snapchat:therock”这样的字眼,它将保存整个字符串

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

根据您的问题描述,听起来您想在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,"//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”来表示用户名。

希望这段代码可以使您入门。

本文链接:https://www.f2er.com/3088476.html

大家都在问