checking_UA.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import time
  2. from selenium import webdriver
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. from fake_useragent import UserAgent
  5. def time_watcher():
  6. print('--- %s seconds ---' % (time.time() - start_time))
  7. def get_data(browser):
  8. try:
  9. ip = browser.find_element_by_class_name("ip-block")
  10. ip_text = ip.find_element_by_class_name('ip').text.strip()
  11. except:
  12. ip = 'Not found'
  13. try:
  14. agent = browser.find_element_by_xpath(
  15. '/html/body/div[4]/table/tbody/tr/td[2]/div/div[2]/span[2]')
  16. agent_text = agent.text.strip()
  17. except:
  18. agent = 'Not found'
  19. data = {'ip': ip_text, 'agent': agent_text}
  20. print(data)
  21. time.sleep(20)
  22. def setup_browser(proxy=None):
  23. # подмена UserAgent
  24. ua = UserAgent()
  25. # user_agent = ua.random
  26. user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36'
  27. # установка proxy
  28. if proxy != None:
  29. PROXY = proxy
  30. webdriver.DesiredCapabilities.CHROME['proxy'] = {
  31. "httpProxy": PROXY,
  32. "ftpProxy": PROXY,
  33. "sslProxy": PROXY,
  34. "proxyType": "MANUAL"}
  35. options = webdriver.ChromeOptions()
  36. options.add_argument(f"--user-agent={user_agent}")
  37. options.add_argument("--start-maximized")
  38. driver = webdriver.Chrome(chrome_options=options,
  39. executable_path=ChromeDriverManager().install())
  40. return driver
  41. def main():
  42. url = 'http://sitespy.ru/my-ip'
  43. proxy = 'http://175.106.18.201:38708'
  44. browser = setup_browser()
  45. browser.get(url)
  46. get_data(browser)
  47. if __name__ == '__main__':
  48. start_time = time.time()
  49. main()
  50. time_watcher()