day05 selenium basic use

The basic use of selenium
from selenium Import the webdriver
# Import keyboard Keys
from selenium.webdriver.common.keys Import Keys
Import Time
'' '
drives two ways browser
' ''
# Script file directly to the first folder to find driving
driver = webdriver.Chrome ()

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

    # send a request to the Home jingdong
    driver.get ( 'https://www.jd.com/')

    # by Find input box input id
    the input_tag = driver.find_element_by_id ( 'key')

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

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

    the time.sleep ( 3)

    '' '
    crawling Jingdong commodity information :
        Doll
            name
            url
            price
            evaluation
    '' '
    # Element to find a
    # elements looking 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 product details page by url attribute selector
        # 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
        Print (good_price)

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


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


    the time.sleep (10)

# catch exceptions
except AS E Exception:
    Print (E)

# will ultimately drive the browser closes off
a finally:
    driver.close ()
# fill in second drive path
# webdriver.Chrome (r'C: \ quartus \ python \ Scripts \ chromedriver.exe ')

Guess you like

Origin www.cnblogs.com/xiaosuen/p/11104522.html