From 03cb250d9a67f34299e7b64b964ae21f67a68c0a Mon Sep 17 00:00:00 2001 From: 0rion <45885529+0rion5@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:48:41 -0700 Subject: [PATCH] Update tinder_bot.py Hey!! i was playing around with trying to index all of the buttons and make them into a dictionary to make the bot navigate tinder. thought this little bit of functionality might make it easier. maybe i might most likely be incorrect. bot = TinderBot() btns = bot.soup.find_all('button') btns_xpath = [bot.xpath_soup(btn) for btn in btns] --- tinder_bot.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tinder_bot.py b/tinder_bot.py index 9eecb7f..3990a46 100644 --- a/tinder_bot.py +++ b/tinder_bot.py @@ -1,12 +1,36 @@ from selenium import webdriver from time import sleep - +from bs4 import BeautifulSoup; import itertools from secrets import username, password class TinderBot(): def __init__(self): self.driver = webdriver.Chrome() - + + @property + def soup(self): + return BeautifulSoup(self.driver.page_source, 'lxml') + + def xpath_soup(self, element): + """ + Generate xpath of soup element + :param element: bs4 text or node + :return: xpath as string + """ + components = list() + child = element if element.name else element.parent + for parent in child.parents: + """ + @type parent: bs4.element.Tag + """ + previous = itertools.islice(parent.children, 0,parent.contents.index(child)) + xpath_tag = child.name + xpath_index = sum(1 for i in previous if i.name == xpath_tag) + 1 + components.append(xpath_tag if xpath_index == 1 else '%s[%d]' % (xpath_tag, xpath_index)) + child = parent + components.reverse() + return '/%s' % '/'.join(components) + def login(self): self.driver.get('https://tinder.com')