from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains import time driver = webdriver.Chrome('E:\chromedriver_win32\chromedriver.exe') try: driver.implicitly_wait(10) driver.get('https://www.jd.com/') time.sleep(3) input = driver.find_element_by_id('key').send_keys('围城') search = driver.find_element_by_class_name('button') search.click() time.sleep(1) input2 = driver.find_element_by_id('key') input2.clear() input2.send_keys('墨菲定律') input2.send_keys(Keys.ENTER) time.sleep(3) finally: driver.close()
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains import time def get_book(driver): try: time.sleep(5) book_list = driver.find_elements_by_class_name('gl-item') for book in book_list: book_name = book.find_element_by_css_selector('.p-name em').text book_url = book.find_element_by_css_selector('.p-name a').get_attribute('href') book_commit = book.find_element_by_class_name('p-commit').text book_price = book.find_element_by_class_name('p-price').text book_content = f''' 商品名称:{book_name} 商品链接:{book_url} 商品价格:{book_price} 商品评价:{book_commit} \n ''' print(book_content) with open('book.text', 'a', encoding='utf-8') as f: f.write(book_content) print('写入成功') #下拉 js_code = ''' window.scrollTo(0,5000) ''' driver.execute_script(js_code) # print('写入成功') # 翻页 next_page = driver.find_element_by_class_name('pn-next') next_page.click() get_book(driver) finally: driver.close() if __name__ == '__main__': driver = webdriver.Chrome('E:\chromedriver_win32\chromedriver.exe') try: driver.implicitly_wait(5) driver.get('https://www.jd.com/') input = driver.find_element_by_id('key') input.send_keys('围城') input.send_keys(Keys.ENTER) time.sleep(5) get_book(driver) finally: driver.close()
浙公网安备 33010602011771号