打印京东商品信息初级版Day03-4

'''
初级版
'''
driver = webdriver.Chrome(r'C:\Users\Administrator\Desktop\chromedriver.exe')
try:
    driver.implicitly_wait(10)
    driver.get('https://jd.com/')

    #往京东输入框输入墨菲定律,按回车键
    input_tag = driver.find_element_by_id('key')
    input_tag.send_keys('墨菲定律')
    input_tag.send_keys(Keys.ENTER)

    time.sleep(5)

    good_list = driver.find_elements_by_class_name('gl-item')
    line = 1
    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'''
        第{line}个商品
        商品名称:{good_name}
        商品链接:{good_url}
        商品价格:{good_price}
        商品评价:{good_commit}
        \n
        \n
        '''
        line += 1

        print(good_content)

        with open('jd3.text','a',encoding='utf-8') as f:
            f.write(good_content)

    print('下载完成')
finally:
    driver.close()

 

posted @ 2019-07-03 18:38  立早声几又香  阅读(138)  评论(0编辑  收藏  举报