from selenium import webdriver
from selenium.webdriver import ChromeOptions
url = 'https://www.hua.com/'
#无头模式(不打开窗口)
# option = ChromeOptions()
# option.add_argument('--headless')
# browser = webdriver.Chrome(options=option)
#模拟打开浏览器
browser = webdriver.Chrome()
browser.get(url)
#窗口最大化
browser.maximize_window()
#1.点击登录按钮
browser.find_element_by_css_selector('#LoginInfo > a:nth-child(1)').click()
#2.点击并输入账号#UserName
browser.find_element_by_css_selector('#UserName').send_keys('771110228@qq.com')
#3.点击并输入密码
browser.find_element_by_css_selector('#PassWord').send_keys('109046@sbq')
#4.点击登录按钮
browser.find_element_by_css_selector('#dosubmit').click()
#5.在搜索框中输入关于友情的花
browser.find_element_by_css_selector('#header > div > div.search-box > form > div > input').send_keys('友情')
#6 点击搜索按钮
browser.find_element_by_css_selector('#header > div > div.search-box > form > div > span > button').click()
#7 爬取数据
all_item=browser.find_element_by_css_selector('body > div.container > div.wrapper > div.pull-left.w970.l230 > div.grid-wrapper') # 使用 find_element_by_css_selector 定位一个
price = all_item.find_elements_by_css_selector('div > div > div.info-cont > div.price > span.price-num') # 代码上移动,定位多个
# 迭代输出text
for item in price:
print(item.text)
|