day03 爬取京东商品信息
''' 初级版: form tank! ''' import time from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() try: driver.implicitly_wait(10) #往京东发送请求 driver.get('http://www.jd.com/') #往京东主页输入框输入墨菲定律,按回车键 input_tag = driver.find_element_by_id('key') input_tag.send_keys('墨菲定律') input_tag.send_keys(Keys.ENTER) time.sleep(5) #js_code = ''' #window. #''' good_list = driver.find_elements_by_class_name('gl-item') for good in good_list: #print(good) #商品名称 good_name = good.find_element_by_css_selector('.p-name em').text print(good_name) #商品链接 good_url = good.find_element_by_css_selector('p-name a').get_attribute('href') print(good_url) #商品价格 good_price = good.find_element_by_css_selector('p-price').text #print(good_price) #商品评价 good_commit = good.find_element_by_css_selector('p_commit').text good_content = f''' 商品名称:{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) print('商品信息写入成功') finally: driver.close()
''' 狂暴版 ''' import time from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() try: driver.implicitly_wait(10) #往京东发送请求 driver.get('http://www.jd.com/') #往京东主页输入框输入墨菲定律,按回车键 input_tag = driver.find_element_by_id('key') input_tag.send_keys('墨菲定律') input_tag.send_keys(Keys.ENTER) time.sleep(5) #js_code = ''' #window. #''' good_list = driver.find_elements_by_class_name('gl-item') for good in good_list: #print(good) #商品名称 good_name = good.find_element_by_css_selector('.p-name em').text print(good_name) #商品链接 good_url = good.find_element_by_css_selector('p-name a').get_attribute('href') print(good_url) #商品价格 good_price = good.find_element_by_css_selector('p-price').text #print(good_price) #商品评价 good_commit = good.find_element_by_css_selector('p_commit').text good_content = f''' 商品名称:{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) print('商品信息写入成功') finally: driver.close()