from selenium import webdriver from selenium.webdriver import ChromeOptions from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys import time option = ChromeOptions() option.add_argument("disable-infobars") def get_goods(driver): num = 400 for line in range(20): js = """ the window.scrollTo (0,% S) "" " % NUM NUM + = 500 driver.execute_script (JS) the time.sleep ( 0.1 ) # . 1 to find all items grandfather tag good_div = driver.find_element_by_id ( " J_goodsList " ) # 2 get all goods li tags good_list = good_div.find_elements_by_class_name ( " GL-Item " ) Print (good_list) for Good in good_list: "" " Product Information: Name Price Links Pictures 评论人数 """ good_name = good.find_element_by_css_selector(".p-name em").text.replace("\n","") good_price = good.find_element_by_css_selector(".p-price").text.replace("\n","") good_link = good.find_element_by_css_selector(".p-img a").get_attribute("href") good_img = good.find_element_by_css_selector('IMG IMG-.p ' ) .get_attribute ( ' the src ' ) # good_img good.find_element_by_css_selector = (. "IMG P-IMG"). get_attribute ( "the src") good_commit = good.find_element_by_css_selector ( " .p-the commit " ). text.replace ( " \ the n- " , "" ) goods = '' ' product information: name% s price% s link% s picture% s number of comments% s ' '' % (good_name, good_price, good_link,good_img,good_commit) print(goods) with open("jd.txt","a",encoding="utf-8")as f: f.write(goods+"\n") next_tag = driver.find_element_by_class_name('pn-next') next_tag.click() time.sleep(2) get_goods(driver) driver = webdriver.Chrome(chrome_options=option) try: driver.get("https://www.jd.com/") driver.implicitly_wait(10) input_tag = driver.find_element_by_id("key") input_tag.send_keys("坦克") #通过回车查找 # input_tag.send_keys(Keys.ENTER) search_button = driver.find_element_by_class_name("button") search_button.click() get_goods(driver) time.sleep(1000) finally: driver.close()