from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
import xlwt
import time
driver = webdriver.Chrome() # 谷歌浏览器
driver.get('https://www.zaixiankaoshi.com/online/?paperId=4940184&practice=&modal=1&is_recite=&qtype=&text=%E9%A1%BA%E5%BA%8F%E7%BB%83%E4%B9%A0&sequence=0&is_collect=0')
driver.implicitly_wait(50)
driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]/p[2]/span[2]/div').click()
work_book = xlwt.Workbook(encoding="UTF-8")
work_sheet = work_book.add_sheet("sheet1")
for i in range(439):
time.sleep(1)
all=[]
# 定位元素并提取内容
answer = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div/div[1]/div/b/span').text
title = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[1]/div/div[1]/div/div').text
part_A = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/div/div[1]/span[2]').text
part_B = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/div/div[2]/span[2]').text
part_C = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/div/div[3]/span[2]').text
part_D = driver.find_element(By.XPATH,'//*[@id="body"]/div[2]/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/div/div[4]/span[2]').text
# 对内容个性化处理
title=str(i+1)+"."+title
part_A = "A." + part_A
part_B = "B." + part_B
part_C = "C." + part_C
part_D = "D." + part_D
answer="答案:"+answer
all.append(title)
all.append(part_A)
all.append(part_B)
all.append(part_C)
all.append(part_D)
all.append(answer)
# 分别写入excel表格中
for ii in range(6):
work_sheet.write(i,ii,all[ii]) # 行 列 内容
# 第1条数据 最大化窗口
if i==0:
driver.maximize_window()
time.sleep(2)
# 点击下一条
driver.find_element(By.CLASS_NAME,'el-button--primary').click()
# 对第10条数据取消收藏处理
if i==9:
driver.find_element(By.XPATH,'/html/body/div[4]/div/div[3]/button[1]').click()
# 存储表格
work_book.save("tt5.xlsx")
# 退出浏览器
driver.quit()