前提: 1.Python是3.9版本 2.anaconda 3安装好后比较方便 3.import的库必须都pip install过 4.本文仅供自己学习使用,请勿随意爬取他人网站,造成的不良后果,本人概不负责 5.秉承开源精神,但转载请注明出处 6.谷歌驱动器下载地址:http://chromedriver.storage.googleapis.com/index.html(安装了他,程序才能调起谷歌浏览器)
代码如下,根据自己运行环境情况调整代码内部分参数
from PIL import Image
import requests
import os
import ddddocr
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import xlrd, xlwt, xlutils.copy
import time
from datetime import datetime
data = xlrd.open_workbook(r"/Users/Desktop/foo.xls")
table = data.sheets()[1]
nrows = table.nrows
workbook = xlwt.Workbook(encoding='utf-8')
sheet1 = workbook.add_sheet(u"new", cell_overwrite_ok = True)
for i in range(nrows):
if i == 0:
continue
username = str(table.cell_value(i,1))
password = username[-5:]
driver=webdriver.Chrome()
driver.get(r'http://xxxx/index.html')
time.sleep(0.3)
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66'
}
driver.maximize_window()
time.sleep(0.3)
driver.find_element_by_xpath('//*[@name="username"]').send_keys(username)
driver.find_element_by_xpath('//*[@name="password"]').send_keys(password)
driver.find_element_by_xpath('//*[@name="veryCode"]').click()
time.sleep(0.5)
driver.save_screenshot('/Users/Desktop/xxx/1.png')
yzm_btn = driver.find_element_by_id('imgObj')
loc = yzm_btn.location
size = yzm_btn.size
left = loc['x']*2
top = loc['y']*2
right = (loc['x'] + size['width'])*2
bottom = (loc['y'] + size['height'])*2
local = (left, top, right, bottom)
pic = Image.open('/Users/Desktop/xxx/1.png')
newimg = pic.crop(local)
newimg.save('/Users/Desktop/xxx/zym.png')
with open('/Users/Desktop/xxx/zym.png', 'rb') as f:
img_bytes = f.read()
ocr = ddddocr.DdddOcr()
ocrCode = ocr.classification(img_bytes)
driver.find_element_by_xpath('//*[@name="veryCode"]').send_keys(ocrCode.upper())
driver.find_element_by_class_name('NormalSubmit').click()
time.sleep(0.5)
driver.get(r"http://xxx.html/二级页面")
time.sleep(1)
mobile = driver.find_element_by_xpath('//*[@name="mobile"]')
find_mobile = mobile.get_attribute("value")
sheet1.write(i, 1, username)
sheet1.write(i, 2, find_mobile)
driver.quit()
workbook.save("/Users/Desktop/zzz.xls")
|