user892134
user892134

Reputation: 3224

Python - TypeError: 'NoneType' object is not subscriptable

I get this error when i try to put an insert query inside while loop

TypeError: 'NoneType' object is not subscriptable

def get_data():
    try:
        conn = mysql.connector.connect(host='localhost',
                                       database='mydb',
                                       user='root',
                                       password='')
        cursor = conn.cursor(buffered=True)
        cursor.execute("SELECT * FROM my_urls WHERE crawl=0")

        row = cursor.fetchone()

        new_data = []

        while row is not None:
            page = requests.get(row[2])

            soup = BeautifulSoup(page.content, 'html.parser')

            #high quality link
            downloads = soup.findAll("li", class_="download-link")
            highq = downloads[-1]
            videofile = highq.find("a").attrs['href']

            #title
            title = soup.find("h1", class_="vone__title")
            title = title.text
            title = title.strip()

            #description
            description = soup.find("div", class_="vone__desc")
            description = description.text
            description = description.strip()

            #video thumbnail
            match = re.search("JSON.parse\(\'(.*)\'\)",page.text)
            thumbnail = ''
            if match:
                thumbnail = json.loads(match.group(1))['poster']

            #meta title
            meta_title = title + " | Test"

            #meta description
            meta_description = "Test."

            #meta keys
            meta_keys = "Test"

            #userid
            user_id = row[1]

            #slug
            slug = title
            slug = slug.replace(" - ", "-")
            slug = re.sub('/\s+/g', '-', slug)
            slug = slug.lower()



            active = 1
            row_data = (user_id,title,description,slug,meta_title,meta_description,meta_keys,active,thumbnail,row[2],'0',videofile)

            sql = "INSERT INTO media (user_id,title,description,slug,meta_title,meta_desc,meta_keys,active,video_thumbnail,ap_link,downloading,ext_filename) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

            cursor.execute(sql, row_data)

            row = cursor.fetchone()



    except Error as e:
        print(e)

    finally:

        conn.commit()      

        cursor.close()
        conn.close()


if __name__ == '__main__':
    get_data()

I've printed the data i want to insert and nothing in the tuple is empty.

This is the line the error occurs

row_data = (user_id,title,description,slug,meta_title,meta_description,meta_keys,active,thumbnail,row[2],'0',videofile)

            sql = "INSERT INTO media (user_id,title,description,slug,meta_title,meta_desc,meta_keys,active,video_thumbnail,ap_link,downloading,ext_filename) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

            cursor.execute(sql, row_data)

Full error:

 File "C:\Users\mich\Desktop\Work\test.py", line 75, in get_data
    row = cursor.fetchone()
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\mysql\connector\cursor_cext.py", line 697, in fetchone
    return self._fetch_row()
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\mysql\connector\cursor_cext.py", line 669, in _fetch_row
    row = self._rows[self._next_row]
TypeError: 'NoneType' object is not subscriptable

How do i solve?

Upvotes: 1

Views: 5326

Answers (1)

Cloudomation
Cloudomation

Reputation: 1662

You cannot use cursor.execute() on a cursor which is being used with cursor.fetchone(). See MySQLCursor.fetchone():

You must fetch all rows for the current query before executing new statements using the same connection.

Try to insert using a second cursor:

def get_data():
    try:
        conn = mysql.connector.connect(host='localhost',
                                       database='mydb',
                                       user='root',
                                       password='')
        cursor = conn.cursor(buffered=True)
        cursor2 = conn.cursor()
        cursor.execute("SELECT * FROM my_urls WHERE crawl=0")

        row = cursor.fetchone()

        new_data = []

        while row is not None:
            page = requests.get(row[2])

            soup = BeautifulSoup(page.content, 'html.parser')

            #high quality link
            downloads = soup.findAll("li", class_="download-link")
            highq = downloads[-1]
            videofile = highq.find("a").attrs['href']

            #title
            title = soup.find("h1", class_="vone__title")
            title = title.text
            title = title.strip()

            #description
            description = soup.find("div", class_="vone__desc")
            description = description.text
            description = description.strip()

            #video thumbnail
            match = re.search("JSON.parse\(\'(.*)\'\)",page.text)
            thumbnail = ''
            if match:
                thumbnail = json.loads(match.group(1))['poster']

            #meta title
            meta_title = title + " | Test"

            #meta description
            meta_description = "Test."

            #meta keys
            meta_keys = "Test"

            #userid
            user_id = row[1]

            #slug
            slug = title
            slug = slug.replace(" - ", "-")
            slug = re.sub('/\s+/g', '-', slug)
            slug = slug.lower()



            active = 1
            row_data = (user_id,title,description,slug,meta_title,meta_description,meta_keys,active,thumbnail,row[2],'0',videofile)

            sql = "INSERT INTO media (user_id,title,description,slug,meta_title,meta_desc,meta_keys,active,video_thumbnail,ap_link,downloading,ext_filename) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

            cursor2.execute(sql, row_data)

            row = cursor.fetchone()



    except Error as e:
        print(e)

    finally:

        conn.commit()      

        cursor.close()
        conn.close()


if __name__ == '__main__':
    get_data()

Upvotes: 2

Related Questions