Python Training Day 5 - reptiles practice

from Selenium Import the webdriver
 # Import keyboard Keys 
from selenium.webdriver.common.keys Import Keys
 Import Time 

Driver = webdriver.Chrome () 

# detector block 
the try :
     # Implicit wait, wait label loading 
    driver.implicitly_wait (10 ) 

    # To jingdong Home transmission request 
    driver.get ( ' https://www.jd.com/ ' ) 

    # Find input by the input box ID 
    The input_tag = driver.find_element_by_id ( ' Key ' ) 

    # send_keys pass the current tag value
    input_tag.send_keys ( ' Chinese Dictionary ' ) 

    # press the keyboard's Enter key 
    input_tag.send_keys (Keys.ENTER) 

    the time.sleep ( 3 ) 

    '' ' 
    crawling Jingdong commodity information: 
        doll 
            name 
            url 
            price 
            evaluation 
    ' '' 
    # Element to find a 
    # Elements find more 
    # find all the products list 
    good_list = driver.find_elements_by_class_name ( ' GL-Item ' )
     # Print (good_list) 

    # loop through each item 
    for Good in good_list:
         #Find by attribute selector product details page URL 
        # URL 
        good_url = good.find_element_by_css_selector ( ' .p IMG-A ' ) .get_attribute ( ' the href ' )
         Print (good_url) 

        # Name 
        good_name = good.find_element_by_css_selector ( ' EM-name .p ' ) .text
         Print (good_name) 

        # price 
        good_price = good.find_element_by_class_name ( ' P-. price ' ) .text
         Print (good_price) 

        # evaluation number 
        good_commit = good.find_element_by_class_name (' P-the commit ' ) .text
         Print (good_commit) 


        str1 = F '' ' 
        URL: {} good_url 
        Name: {good_name} 
        Price: {good_price} 
        Evaluation: good_commit} { 
        \ n- 
        ' '' 
        # the commodity information written text in 
        with Open ( ' jd.txt ' , ' A ' , encoding = ' UTF-. 8 ' ) AS F: 
            f.write (str1) 


    the time.sleep ( 10 ) 

# catch exception 
the except exception AS E:
     Print (E)

# Will ultimately drive the browser closes off 
a finally : 
    driver.close ()

 

Guess you like

Origin www.cnblogs.com/dadahappy/p/11103029.html