utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from utils.retry import (
  2. retry_func,
  3. locate_element_with_retry,
  4. find_clickable_element_with_retry,
  5. )
  6. from time import sleep
  7. import re
  8. from bs4 import BeautifulSoup
  9. from utils.config import config
  10. if config.open_driver:
  11. try:
  12. from selenium.webdriver.common.by import By
  13. except:
  14. pass
  15. def get_soup_driver(url):
  16. """
  17. Get the soup by driver
  18. """
  19. from driver.setup import setup_driver
  20. driver = setup_driver()
  21. retry_func(lambda: driver.get(url), name=url)
  22. sleep(1)
  23. source = re.sub(
  24. r"<!--.*?-->",
  25. "",
  26. driver.page_source,
  27. flags=re.DOTALL,
  28. )
  29. soup = BeautifulSoup(source, "html.parser")
  30. driver.close()
  31. driver.quit()
  32. return soup
  33. def search_submit(driver, name):
  34. """
  35. Input key word and submit with driver
  36. """
  37. search_box = locate_element_with_retry(driver, (By.XPATH, '//input[@type="text"]'))
  38. if not search_box:
  39. return
  40. search_box.clear()
  41. search_box.send_keys(name)
  42. submit_button = find_clickable_element_with_retry(
  43. driver, (By.XPATH, '//input[@type="submit"]')
  44. )
  45. if not submit_button:
  46. return
  47. driver.execute_script("arguments[0].click();", submit_button)