学习爬虫第三天

时间:2020-12-17 20:51:55
实现京东搜索“公仔”并关闭浏览器
并查找公仔、名称、url、价格、评价
from selenium import webdriver
#导入键盘Keys
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
try:
#隐式等待,等待标签加载
driver.implicitly_wait(10)

#往京东主页发送请求
driver.get('https://www.jd.com/')

#通过id查找imput输入框
input_tag = driver.find_element_by_id('key')

#send_keys为当前标签传值
input_tag.send_keys('考研数学')

#按键盘的回车键
input_tag.send_keys(Keys.ENTER)
'''
爬取京东商品信息:
公仔
名称
url
价格
评价
'''
#element找一个
#elements找多个
good_list = driver.find_element_by_class_name('g1-item')
for good in good_list:
good_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').text
print(good_name)
good_price = driver.find_element_by_class_name('g1-item')
print(good_price)
good_commit = driver.find_element_by_class_name('g1-item')
print(good_commit)
time.sleep(10)

except Exception as e:
print(e)
#最后都会把驱动浏览器关闭掉
# finally:
# driver.close()