python的requests出现10053错误, 你的主机中的软件中止了一个已建立的连接可能的出错原因

最近在做一个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)

报错信息: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。

第一次循环可以完美运行, 第二次循环报错。

出BUG了还解决不了能怎么办,百度大法好!ctrl+c 报错信息, but, 又一次 too young too naive, 个中幸酸就不说了, 结果就是百度没有帮到我, 废话说了这么多, 说结果吧:

实例化的driver不要放在__init__方法里面, 放到下面的其他方法里面, 个人的理解是第一次循环开始浏览器没有做过关闭操作, 但是第一次循环结束(第二次循环开始之前)浏览器进行关闭操作, 造成这个初始化实例(全局的)的状态已经被改变, 纯属个人理解(瞎说的), 希望遇到同样问题的coder可以借鉴一下, 同时如果帖子有幸被大神瞥到希望赐教一下原因, 让我也体验一下知其所以然的滋味。

 

 

 

你可能感兴趣的:(python的requests出现10053错误, 你的主机中的软件中止了一个已建立的连接可能的出错原因)