0

I want to make a discord bot, that gets certain codes from a website and posts them every day at 12am in a certain channel. I got the web scraping thing to work, but I don't know how to implement that into my discord bot, or if it's even possible. I need to know how I can send the results from the scraping as a message on discord. I made this with a Wayscript tutorial from youtube and a tutorial for unordered lists on stackoverflow:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0')

browser = webdriver.Chrome(options = options)
browser.set_page_load_timeout(30)

browser.get('https://www.pockettactics.com/genshin-impact/codes')

codes = browser.find_element_by_class_name('entry-content')
codes_element = codes.find_elements_by_tag_name('ul')
temp = codes_element[0].find_elements_by_tag_name('li')

print(temp)

browser.close()

It prints this:

[<selenium.webdriver.remote.webelement.WebElement (session="cce994f1f2ff0ba3ec16090d353a8124", element="e2ca46c2-4e26-4039-95d3-e6e3fc17ca2f")>, <selenium.webdriver.remote.webelement.WebElement (session="cce994f1f2ff0ba3ec16090d353a8124", element="0b29d818-4678-4108-9d05-6336fe0d6c8a")>]

So I think it finds the two codes listed in the elements, but I'm not sure how I can get my bot to send the contents of those results as a message.

I'm very new to web scraping, using python and making discord bots, so I have no idea what to do here and I can't find answers anywhere. I have tried a million different ways to do this but nothing works. The closest I got was some weird error as a message, so it kinda worked but it didn't send the actual contents of the element I was looking for.

wilzzu
  • 1

1 Answers1

0

These are:

[<selenium.webdriver.remote.webelement.WebElement (session="cce994f1f2ff0ba3ec16090d353a8124", element="e2ca46c2-4e26-4039-95d3-e6e3fc17ca2f")>, <selenium.webdriver.remote.webelement.WebElement (session="cce994f1f2ff0ba3ec16090d353a8124", element="0b29d818-4678-4108-9d05-6336fe0d6c8a")>]

the WebElements when you print them on the console.


To print the desired texts you can use the following locator strategy:

  • Using CSS_SELECTOR:

    driver.get("https://www.pockettactics.com/genshin-impact/codes")
    print(driver.find_element(By.CSS_SELECTOR, ".entry-content ul:nth-of-type(1) li").text)
    

To extract the texts ideally you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following locator strategy:

  • Using CSS_SELECTOR:

    driver.get("https://www.pockettactics.com/genshin-impact/codes")
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".entry-content ul:nth-of-type(1) li")))])
    
  • Console Output:

    ['ZSPDKSC3V8V5 – 60 primogems and five adventurer’s experience (new!)', 'GENSHINGIFT – 50 primogems and three hero’s wit (this code works periodically)']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352