今天闲着折腾了一下Selenium框架 ,挺有趣的。这个框架可以驱动浏览器执行网页操作,就好像是人在操作一样,可以用来实现一些爬虫。下面是我折腾了一会儿弄出来的Demo代码,可以在需要的时候复用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 from selenium import webdriverfrom selenium.webdriver import Keysfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byfrom webdriver_manager.chrome import ChromeDriverManagerdef get_driver (): options = webdriver.ChromeOptions() options.add_argument('--headless' ) options.add_argument('--window-size=1920,1080' ) options.add_argument(r"user-data-dir=C:\Users\xxx\AppData\Local\Google\Chrome\User Data" ) service = Service(ChromeDriverManager().install()) return webdriver.Chrome(service=service, options=options) def swith_to_title (dirver, title ): for handle in driver.window_handles: driver.switch_to.window(handle) if title in driver.title: return handle if __name__ == "__main__" : driver = get_driver() driver.get('https://www.bilibili.com/' ) driver.find_elements(By.XPATH, '// *[ @ id = "nav_searchform"] / input' )[0 ].send_keys("郭德纲" ) driver.find_elements(By.XPATH, '// *[ @ id = "nav_searchform"] / input' )[0 ].send_keys(Keys.ENTER) swith_to_title(driver, "郭德纲" ) print(driver.page_source)