一 实现京东上的自动搜索并提取信息
from selenium import webdriver # 导入键盘Keys from selenium.webdriver.common.keys import Keys import timedriver = webdriver.Chrome()# 检测代码块 try:# 隐式等待,等待标签加载driver.implicitly_wait(10)# 往京东主页发送请求driver.get('https://www.jd.com/')# 通过id查找input输入框input_tag = driver.find_element_by_id('key')# send_keys为当前标签传值input_tag.send_keys('中华字典')# 按键盘的回车键input_tag.send_keys(Keys.ENTER)time.sleep(3)''' 爬取京东商品信息:公仔名称url价格评价''' # element 找一个# elements 找多个# 查找所有的商品列表good_list = driver.find_elements_by_class_name('gl-item')# print(good_list)# 循环遍历每一个商品for good in good_list:# 通过属性选择器查找商品详情页url# urlgood_url = good.find_element_by_css_selector('.p-img a').get_attribute('href')print(good_url)# 名称good_name = good.find_element_by_css_selector('.p-name em').textprint(good_name)# 价格good_price = good.find_element_by_class_name('p-price').textprint(good_price)# 评价数good_commit = good.find_element_by_class_name('p-commit').textprint(good_commit)str1 = f''' url: {good_url}名称: {good_name}价格: {good_price}评价: {good_commit}\n''' # 把商品信息写入文本中with open('jd.txt', 'a', encoding='utf-8') as f:f.write(str1)time.sleep(10)# 捕获异常 except Exception as e:print(e)# 最后都会把驱动浏览器关闭掉 finally:driver.close()