day2 python从入门到放弃----爬取京东商品信息 +元素交互操作

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}
            商品名称:{good_name}
            商品连接:{good_url}
            商品价格:{good_price}
            商品评价:{good_commit}
            \n
            '''
            print(good_content)
            with open('jd.txt','a',encoding='utf-8') as f:
                f.write(good_content)
            num += 1
        print('商品信息写入成功')
        # 查找下一页并点击
        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()

猜你喜欢

转载自www.cnblogs.com/tankfaledeblog/p/11129507.html
今日推荐