Day.05 selenium use

Import the webdriver Selenium from 
# 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 ( 'doll ') 

    # Enter key of the keyboard 
    input_tag.send_keys (Keys.ENTER) 

    the time.sleep (. 3) 

    ' '' 
    crawling Jingdong product information: 
        doll 
            url 
            price 
            evaluation 
    '' ' 
    # element to find a 
    # Elements plurality find 
    # find a list of all commodities 
    good_list = driver.find_elements_by_class_name ( 'GL-Item') 
    # Print (good_list) 

    # loop through each item 
    for in good_list Good: 

        # Find items through attribute selector details page URL 
        # URL 
        good_url = good.find_element_by_css_selector ( '. IMG P-A'). get_attribute ( 'the href') 
        Print (good_url) 

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

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

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

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

    the time.sleep (10) 

# catch exception 
except exception as E: 
    Print (E) 

# will ultimately drive the browser closes off 
a finally: 
    driver.close ()
Copy the code

Guess you like

Origin www.cnblogs.com/promiss911/p/11105120.html