如何在网站上找到部分字符串并保存
How to find part of string on website and save it
所以我想为自己制作一个小型 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,比如 'Snapchat:therock',它将保存整个字符串
根据您的问题描述,您似乎想在 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% 地找到 snapchat 用户名基本上是不可能的。每个人的 Instagram 简历格式都不同,也许他们用 "sc: mysnapchat" 而不是 "Snapchat:therock" 来表示他们的用户名。
希望这段代码可以帮助您开始某个地方。
所以我想为自己制作一个小型 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,比如 'Snapchat:therock',它将保存整个字符串
根据您的问题描述,您似乎想在 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% 地找到 snapchat 用户名基本上是不可能的。每个人的 Instagram 简历格式都不同,也许他们用 "sc: mysnapchat" 而不是 "Snapchat:therock" 来表示他们的用户名。
希望这段代码可以帮助您开始某个地方。