‘‘‘初级版: form tank!‘‘‘import timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = 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 timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = 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‘,enCoding=‘utf-8‘)as f: f.write(good_content) print(‘商品信息写入成功‘)finally: driver.close()总结
以上是内存溢出为你收集整理的day03 爬取京东商品信息全部内容,希望文章能够帮你解决day03 爬取京东商品信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)