Python Forum

Full Version: Why am I getting this error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to add some data from a file, but getting this error.

Traceback (most recent call last):
  File "spider.py", line 142, in <module>
    main()
  File "spider.py", line 140, in main
    addQuestion()
  File "spider.py", line 97, in addQuestion
    cur.execute(sql, (str(id_list.pop()), line.rstrip()))
  File "/home/mikefromru/django_apps/practice-speaking-en/env/lib/python3.5/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/home/mikefromru/django_apps/practice-speaking-en/env/lib/python3.5/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/home/mikefromru/django_apps/practice-speaking-en/env/lib/python3.5/site-packages/pymysql/connections.py", line 515, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/home/mikefromru/django_apps/practice-speaking-en/env/lib/python3.5/site-packages/pymysql/connections.py", line 745, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')
import requests
import os
from bs4 import BeautifulSoup
#import sqlite3
import pymysql.cursors

conn = pymysql.connect(host='localhost',
    user='root',
    password='password',
    db='english_speaking',
    cursorclass=pymysql.cursors.DictCursor)

cur = conn.cursor()

def addQuestion():
    links_list = open('links.txt').readlines()
    id_list = [x for x in range(1545, 1785)]

    try:
        while len(id_list) !=0:
            #print('{}: {}'.format(id_list.pop(), links_list.pop().rstrip()))

            s = []
            url = 'http://iteslj.org/questions/' + links_list.pop().rstrip()
            
            r = requests.get(url)
            soup = BeautifulSoup(r.content, 'lxml')
            ul = soup.find_all('li')
            
            for x in ul:
                s.append(x.get_text().split('\n'))

            f = open('right_file.txt', 'w')
            new_s = []
            for x in s:
                if len(x) == 2:
                    x.pop()
                    new_s.append(x)

            for x in new_s:
                f.write(''.join(x) + '\n')
            f.close()

            read_file = open('right_file.txt')

            line = read_file.readline()
            while line:
                sql = "insert into `question`(`subject_id`, `name`) values (%s, %s)"
                cur.execute(sql, (str(id_list[-1]), line.rstrip()))
                line = read_file.readline()
                conn.commit()
            id_list.pop()
            conn.close()
            read_file.close()

    except IndexError as error:
        print('done')
How can I fixit it?
I resoleved it.
I should move
conn.close()
into except
except IndexError:
    conn.close()     
    print('done')