tools.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import re
  2. from time import sleep
  3. from bs4 import BeautifulSoup
  4. from utils.config import config
  5. from utils.retry import (
  6. retry_func,
  7. locate_element_with_retry,
  8. find_clickable_element_with_retry,
  9. )
  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 utils.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)