最近在做一个python爬虫项目需要写一些自动化的脚本, 想着简单的小脚本几个小时写完就可以吃着火锅唱着歌了, 然而还是too young too naive, 废话不多说先上出错的代码部分和报错:
class main():
def __init__(self):
super(main, self).__init__()
self.conn = pymysql.connect(host='localhost', port=3306,
user='root', password='********',
db='pinterestall', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.conn1 = pymysql.connect(host='localhost', port=3306,
user='root', password='*******',
db='store', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.driver = webdriver.Chrome()
self.hostname = socket.gethostname()
self.check__num = 0
self.email = ''
self.pwd = ''
self.cookie = ''
self. = ''
self.account_id = 0
self.success_num = 0
self.upload_pic_min_num = 3
self.upload_pic_max_num = 5
self.current_time = datetime.datetime.now().strftime("%Y-%m-%d")
self.pinterestAcotion()
def pinterestAcotion(self):
while True:
if self.success_num > 4:
os.system('shutdown -r')
print('clear cache')
time.sleep(9999)
writeTxtTime()
print(self.hostname)
print('Access to the account:')
self.getAccount()
# print(self.account_id)
if self.account_id > 0:
self.success_num += 1
self.connectVPN()
login_state = login(
self.driver, self.email, self.pwd, self.account_id, self.cookie, self.conn)
# print(login_state)
if login_state == 1:
sql = "update account set login_times = login_times + 1 where id=%s" % self.account_id
else:
sql = "update account set state = 9 , login_times = 0 where id=%s" % self.account_id
writeSQL(self.conn, sql)
if login_state == 0:
self.driver.quit()
time.sleep(5)
continue
else:
sql = "update account set state = 1, action_time='%s' where id=%s" % (
self.current_time, self.account_id)
writeSQL(self.conn, sql)
try:
time.sleep(5)
pred = len(self.driver.find_elements_by_xpath(
'//div[@class="interestCardWrapper"]'))
# '//button[@class="NuxInterest"]'
for i in range(pred):
if i > 5:
break
self.driver.find_elements_by_xpath(
'//div[@class="interestCardWrapper"][i+1]').click()
time.sleep(2)
self.driver.find_element_by_xpath(
'//div[@class="NuxPickerFooter"]//button').click()
time.sleep(3)
self.driver.find_element_by_class_name(
'NuxExtensionUpsell__optionalSkip underline').click()
time.sleep(3)
except Exception as e:
print('没有偏好设置, 跳过...', e)
time.sleep(2)
try:
self.driver.find_element_by_xpath(
'//div[@class="ReactModalPortal"]//button').click()
time.sleep(2)
except Exception as e:
print('无需确认邮箱, 跳过...', e)
time.sleep(2)
try:
click_confirm = self.driver.switch_to_alert()
time.sleep(2)
click_confirm.accept()
time.sleep(2)
except Exception as e:
print('没有需要处理的弹窗, 跳过...', e)
time.sleep(2)
# self.uploadPic()
self.randomBrowsing()
print('End of account processing...')
self.driver.quit()
writeTxtTime()
time.sleep(10)
else:
print('Not data...')
writeTxtTime()
time.sleep(60)
def randomBrowsing(self):
random_browsing_num = random.randint(2, 6)
print('开始随机浏览:', random_browsing_num, '次')
for i in range(random_browsing_num):
try:
web_pin_arr = self.driver.find_elements_by_xpath("//div[@class='pinWrapper']")
click_num = random.randint(1, 8)
print('开始第', i + 1, '次浏览,点击页面第', click_num, '个pin...')
web_pin_num = 1
for web_pin_one in web_pin_arr:
if web_pin_num == click_num:
web_pin_one.click()
time.sleep(5)
self.driver.execute_script('window.scrollTo(1, 3000)')
time.sleep(2)
break
else:
web_pin_num += 1
time.sleep(3)
except Exception as e:
print(e)
第一次循环可以完美运行, 第二次循环报错。
出BUG了还解决不了能怎么办,百度大法好!ctrl+c 报错信息, but, 又一次 too young too naive, 个中幸酸就不说了, 结果就是百度没有帮到我, 废话说了这么多, 说结果吧: