1、调不起chrome浏览器
需下载自己浏览器对应版本的浏览器驱动chromedriver.exe并放至python路径,下载地址:ChromeDriver Mirrorhttp://npm.taobao.org/mirrors/chromedriver/
2、浏览器打开多个窗口/页面弹出新页面,如何定位新页面
time.sleep(3)#切窗口操作前,一定要加上延时,待上一次点击操作要打开的窗口先打开
handles = driver.window_handles # 获取当前浏览器的所有窗口句柄
driver.switch_to.window(handles[-1]) # 切换到最新打开的窗口
driver.switch_to.window(handles[-2]) # 切换到倒数第二个打开的窗口
driver.switch_to.window(handles[0]) # 切换到最开始打开的窗口
3、定位一个元素有两个值:
goods_stock_all = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[1]/div')[0].text
goods_stock_2 = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[1]/div/span')[0].text
order_goods_number_all = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[2]/div')[0].text
order_goods_number_2 = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[2]/div/span')[0].text
goods_stock = goods_stock_all.replace(goods_stock_2, '')#元素定位取到2个值,将后面的值剔除
order_goods_number = order_goods_number_all.replace(order_goods_number_2, '')
样例:
#coding=utf-8
import time,MySQLdb
from selenium import webdriver
chrome_driver=r"C:\wh\python\chrome\chromedriver.exe"#需要打开的浏览器驱动路径
#输入需要监控的主播名
anchor_name = '大'
class TopGoods():
def getTopGoods(anchor_name):
print('=============开始打开浏览器获取页面数据==============)')
driver = webdriver.Chrome(executable_path=chrome_driver)
driver.maximize_window() # 最大化浏览器
driver.implicitly_wait(8) # 设置隐式时间等待
driver.get("https://y.com/liveDataManage/DMAFC1E9EED911255430917301306431")
try:
driver.find_elements_by_id("basic_userPhone")[0].send_keys("18600000000")
driver.find_elements_by_id("basic_userPasswd")[0].send_keys("123456")
driver.find_elements_by_xpath('//*[@id="basic"]/div[3]/div/div/div/button')[0].click()
driver.find_element_by_xpath("//*[contains(text(),'%s')]" %(anchor_name)).click()#通过传入的主播名模糊匹配进入该场次
except Exception as e:
return e
print("登录成功,进入直播场控")
time.sleep(3)#切窗口操作前,一定要加上延时,待上一次点击操作要打开的窗口先打开
handles = driver.window_handles # 获取当前浏览器的所有窗口句柄
driver.switch_to.window(handles[-1]) # 切换到最新打开的窗口
# driver.switch_to.window(handles[-2]) # 切换到倒数第二个打开的窗口
# driver.switch_to.window(handles[0]) # 切换到最开始打开的窗口
time.sleep(3)
driver.find_element_by_xpath("//*[contains(text(),'主播大屏')]").click()
db = MySQLdb.connect(host='192.168.122.208', user='root', password='123456', database='ywwl_python_test', charset='utf8')
A = db.cursor()
AA = 1
while AA < 10:
time.sleep(6)
try:
online_number = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[2]/div[2]/div[3]/div[2]')[0].text
total_sales = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[2]/div[2]/div[2]/span')[0].text
Popular = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[1]/div[2]/span[1]')[0].text
tag = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[2]/div[1]/div')[0].text
goods_name = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[2]/div[2]/p[1]')[0].text
goods_price = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[2]/div[2]/p[2]')[0].text
goods_stock_all = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[1]/div')[0].text
goods_stock_2 = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[1]/div/span')[0].text
order_goods_number_all = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[2]/div')[0].text
order_goods_number_2 = driver.find_elements_by_xpath('//*[@id="ywAppRoot"]/div/div/div[3]/div[2]/div[3]/div[1]/div[2]/div[2]/div/span')[0].text
goods_stock = goods_stock_all.replace(goods_stock_2, '')#元素定位取到2个值,将后面的值剔除
order_goods_number = order_goods_number_all.replace(order_goods_number_2, '')
#将数值格式化,去除¥,。
total_sales,goods_price,goods_stock,order_goods_number = total_sales.replace('¥', ''),goods_price.replace('¥', ''),goods_stock.replace(',', ''),order_goods_number.replace(',', '')
total_sales = total_sales.replace(',', '')
sql = "INSERT INTO livedata_top_goods (anchor_name,online_number,total_sales,Popular,tag,goods_name,goods_price,goods_stock,order_goods_number,STATUS,createTime,updateTime)VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','1',NOW(),NOW())" %(anchor_name,online_number,total_sales,Popular,tag,goods_name,goods_price,goods_stock,order_goods_number)
A.execute(sql)
db.commit()
AA = AA + 1
except Exception as e:
db.close()# 关闭数据库连接
driver.quit()#退出浏览器
return e
else:
db.close() # 关闭数据库连接
driver.quit() # 退出浏览器
return '========执行结束=========='
print(TopGoods.getTopGoods(anchor_name))