首页 > 编程语言 > 详细

Python操作selenium抓取数据到Excel案例

时间:2020-10-26 19:45:21      阅读:66      评论:0      收藏:0      [点我收藏+]

废话不多说直接上代码 

import time
from telnetlib import EC

import xlwt as xlwt
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# option=webdriver.ChromeOptions()
# option.add_argument(‘--headless‘) 设置后台运行
browser = webdriver.Chrome()#chrome_options=option
browser.maximize_window() #设置浏览器全屏
browser.get("*******************") #要操作的url
def isElementExist(id):
    try:
        browser.find_element_by_id("details-button") #下面""里面都是你要操作页面的定位
        return True
    except:
        return False
def isElementExistx(id):
    try:
        browser.find_elements_by_class_name("dt-modal-close")[0]
        return True
    except:
        return False
if(isElementExist(browser)):
   browser.find_element_by_id("details-button").click()
   browser.find_element_by_id("proceed-link").click()
   browser.find_element_by_id("login-by").click()


browser.find_element_by_id("login-by").send_keys("st_sm_d5793")
browser.find_element_by_id("password").send_keys("dzm@19980525")
browser.find_element_by_id("btn-submit-login").click()
browser.implicitly_wait(30)
if(isElementExistx(browser)):
   browser.find_elements_by_class_name("dt-modal-close")[0].click()#点击提示差
time.sleep(0.5)
article =browser.find_elements_by_class_name("dtdrc-topnav-current-tenant")[0]
ActionChains(browser).move_to_element(article).perform()
time.sleep(0.5)
browser.find_elements_by_class_name("dtdrc-topnav-tenant-tooltip-item")[0].click()
browser.find_elements_by_class_name("dtd-pro-main-webapp-react-src-components-sider-menu-index-customLink")[0].click()
browser.implicitly_wait(10)
browser.find_element_by_xpath(//*[@id="root"]/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/div/form/div/div[2]/div/div[1]/div/div[2]/div/span[6]/span).click()
browser.implicitly_wait(10)
browser.find_element_by_xpath(//*[@id="root"]/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/div/form/div/div[2]/div/div[1]/div/div[2]/div/span[8]/span).click()
browser.implicitly_wait(10)
browser.find_element_by_xpath(//*[@id="customCol"]/div/div/div/div[2]/ul/li[2]/div[1]/div).click()
browser.find_elements_by_class_name("dt-select-dropdown-menu-item")[3].click()
browser.implicitly_wait(10)
flag=True
index=1
trnlist=[]#转换作业名称列表
createlist=[]#创建者名称列表
types=[]#状态列表
comments=[]#备注列表
#转换名称判断
def trnname(id):
    try:
        browser.find_element_by_xpath(//*[@id="customCol"]/div/div/div/div[1]/div/div/div/table/tbody/tr[ + str(index) + ]/td[2]/a)
        return True
    except:
        return False
while(flag):
    if(trnname(browser)):
        zhuanname=browser.find_element_by_xpath(//*[@id="customCol"]/div/div/div/div[1]/div/div/div/table/tbody/tr[ + str(index) + ]/td[2]/a).text
        trnlist.append(zhuanname)
        createname = browser.find_element_by_xpath(//*[@id="customCol"]/div[1]/div/div/div[1]/div/div/div/table/tbody/tr[ + str(index) + ]/td[3]).text
        createlist.append(createname)
        type=browser.find_element_by_xpath(//*[@id="customCol"]/div/div/div/div[1]/div/div/div/table/tbody/tr[ + str(index) + ]/td[4]/span/span[2]).text
        types.append(type)
        comment=browser.find_element_by_xpath(//*[@id="customCol"]/div/div/div/div[1]/div/div/div/table/tbody/tr[ + str(index) + ]/td[5]/span).text
        comments.append(comment)
        index+=1
    else:
        flag=False

current_date=time.strftime("%Y/%m/%d")
workbook = xlwt.Workbook(encoding = utf-8)
worksheet = workbook.add_sheet(current_date)
row=1
col=0
one="转换英文名"
worksheet.write(0,0,one)
for item in trnlist:
    worksheet.write(row, col, item)
    row += 1
row=1
two="创建者"
worksheet.write(0,1,two)
for item in createlist:
    worksheet.write(row, 1, item)
    row += 1
row=1
three="转换备注"
worksheet.write(0,2,three)
for item in comments:
    worksheet.write(row,2,item)
    row +=1
row=1
four="转换状态"
worksheet.write(0,3,four)
for item in types:
    worksheet.write(row,3,item)
    row +=1
workbook.save(E:\\test.xls)
# ActionChains(browser).move_by_offset(200, 100).click().perform()#鼠标左键在空白处点击
# ActionChains(dr).move_by_offset(200, 100).context_click().perform() # 鼠标右键点击


#browser.close()关闭浏览器

 

Python操作selenium抓取数据到Excel案例

原文:https://www.cnblogs.com/tkzm/p/13880051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!