day2 python from entry to abandon ---- crawling Jingdong commodity information elements interact +

from selenium import webdriver
from selenium.webdriver.common.keys import Keys  #键盘按键操作
import time
def get_good(driver):
    num=1
    try:
        time.sleep(5)
        # 下拉滑动5000px
        js_code ='''
        window.scrollTo(0,5000)
        '''
        driver.execute_script(js_code)
        time.sleep(5)
        good_list = driver.find_elements_by_class_name('gl-item')
        for good in good_list:
            good_name = good.find_element_by_css_selector('.p-name em').text
            good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')
            good_price = good.find_element_by_class_name('p-price').text
            good_commit = good.find_element_by_class_name('p-commit').text
            good_content = f'''
            num: {num} 
            Product Name: {good_name} 
            Goods connected: {good_url} 
            commodity prices: {good_price} 
            product reviews: good_commit} { 
            \ n- 
            '' ' 
            Print (good_content) 
            with Open ( ' jd.txt ' , ' A ' , encoding = ' UTF-8 ' ) AS f: 
                f.write (good_content) 
            NUM + 1 =
         Print ( ' commodity information is written to success ' )
         # Find Next and click 
        next_tag = driver.find_element_by_class_name ( 'pn-next')
        next_tag.click()
        time.sleep(5)
        # 递归调用函数本身
        get_good(driver)
    finally:
        driver.close()
if __name__ == '__main__':
    driver = webdriver.Chrome(r'D:\Program Files\Python\Python36\Scripts\chromedriver.exe')
    try:
        driver.implicitly_wait(10)
        driver.get('https://www.jd.com/')
        input_tag = driver.find_element_by_id('key')
        input_tag.send_keys('墨菲定律')
        input_tag.send_keys(Keys.ENTER)
        get_good(driver)
    finally:
        driver.close()

 

Guess you like

Origin www.cnblogs.com/tankfaledeblog/p/11129507.html