1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
import time import random from selenium import webdriver import xlwt from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC
frontCar = '1234' countNum = 0
class_one = ['小马'] class_two = ['大飞', '小猪'] class_three = ['李四', '王五'] class_four = ["张三"]
def DengLu(driver): print('即将打开登录界面')
driver.get("你的查验url")
driver.find_element_by_xpath('/html/body/ngx-app/rt-login/div[2]/div[2]/input').send_keys( "改为你的账号")
driver.find_element_by_xpath('/html/body/ngx-app/rt-login/div[2]/div[3]/input').send_keys("改为你的密码")
print("请手动输入图片验证码并登录...") print("登录后请点开需要查验的第一辆...")
while True: try: n = int(input('请输入拉取得数据个数:')) if n > 5000: print("数字太大了,请重新输入:") continue break except ValueError: print('输入的不是整数,请重新输入:')
return n
def StartGetDate(driver, fp, sheet, i, n): global frontCar, countNum
ChaYanFullTime = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span').text
chepai = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span').text
localCar = ChaYanFullTime + chepai
while frontCar == localCar: try: driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/div[2]/img[1]').click() element = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.XPATH, '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span')) ) element = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.XPATH, '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span')) ) element = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.XPATH, '//*[@id="app-root"]/app-main-layout')) ) time.sleep(1) except find_element_by_class_xpath: print("已经全部拉取完毕。") finally: time.sleep(random.randint(1, 2))
ChaYanFullTime = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span').text
chepai = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span').text
localCar = ChaYanFullTime + chepai
ChaYanDate = ChaYanFullTime.split(' ')[0]
ChaYanTime = ChaYanFullTime.split(' ')[-1]
cheliangleixing = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[5]/span').text
jinE = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[8]/td[3]/span').text
shouFeiYuan = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[11]/td[7]/span').text
jiChaYuan = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[12]/td[3]/span').text
if jiChaYuan in class_three: banci = 3 elif jiChaYuan in class_one: banci = 1 elif jiChaYuan in class_two: banci = 2 elif jiChaYuan in class_four: banci = 4 else: banci = None
chayanResult = driver.find_element_by_xpath( '//*[@id="app-root"]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[9]/td[3]/span').text
print(i + 1, "/", n, ': ', banci, ChaYanFullTime, shouFeiYuan, jiChaYuan, chepai, cheliangleixing, jinE, end=' ') print(ChaYanFullTime, banci, shouFeiYuan, jiChaYuan, chepai, cheliangleixing, jinE, end=' ', file=fp)
sheet.write(i + 2, 0, i + 1) sheet.write(i + 2, 1, ChaYanDate) sheet.write(i + 2, 2, ChaYanTime) sheet.write(i + 2, 3, banci) sheet.write(i + 2, 5, shouFeiYuan) sheet.write(i + 2, 6, jiChaYuan) sheet.write(i + 2, 7, chepai) sheet.write(i + 2, 8, cheliangleixing)
if chayanResult == '不合格': buhege = driver.find_element_by_xpath( '/html/body/ngx-app/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[10]/td[3]/span').text print(buhege, end=' ') print(buhege, end=' ', file=fp) sheet.write(i + 2, 9, buhege) else: print(end=' ') print(end=' ', file=fp) sheet.write(i + 2, 9, '')
sheet.write(i + 2, 10, jinE)
print(end='\n') print(end='\n', file=fp)
frontCar = localCar
countNum = countNum + 1
def main():
options = Options()
options.add_argument('--window-size=1366,768')
print('Launch browser') driver = webdriver.Chrome(chrome_options=options)
print('Delete cookies') driver.delete_all_cookies()
n = DengLu(driver)
startTime = time.time()
fp = open(r"temp.txt", "w+", encoding="utf-8") workbook = xlwt.Workbook() sheet = workbook.add_sheet("sheet1")
for i in range(n): StartGetDate(driver, fp, sheet, i, n) if i % 10 == 9: workbook.save('temp.xls')
fp.close() workbook.save('temp.xls')
driver.quit()
endTime = time.time() print("共 %d 辆" % countNum) print("共花费时间: %d 秒" % (endTime - startTime))
if __name__ == '__main__': main()
|