#python
#python
Вопрос:
Я получаю эту ошибку, когда пытаюсь вставить запрос insert внутри цикла while
Ошибка типа: объект ‘NoneType’ не подлежит подписке
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()
Я напечатал данные, которые хочу вставить, и ничто в кортеже не является пустым.
В этой строке возникает ошибка
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)
Полная ошибка:
File "C:UsersmichDesktopWorktest.py", line 75, in get_data
row = cursor.fetchone()
File "C:laragonbinpythonpython-3.6.1libsite-packagesmysqlconnectorcursor_cext.py", line 697, in fetchone
return self._fetch_row()
File "C:laragonbinpythonpython-3.6.1libsite-packagesmysqlconnectorcursor_cext.py", line 669, in _fetch_row
row = self._rows[self._next_row]
TypeError: 'NoneType' object is not subscriptable
Как мне решить?
Комментарии:
1. Вы должны проверить,
row = cursor.fetchone()
возвращаетNone
ли он, похоже, что проблема заключается именно в этом.2. Похоже, что
row
переменнаяNone
— но я вижу, что она тоже индексируется раньше. Можете ли вы описать всю ошибку целиком?3. @DroidX86 проверьте редактирование, я добавил полную ошибку
4. Похоже, что ваш запрос не возвращает никаких строк
Ответ №1:
Вы не можете использовать cursor.execute()
для курсора, который используется с cursor.fetchone()
. Смотрите MySQLCursor.fetchone():
Вы должны извлечь все строки для текущего запроса перед выполнением новых инструкций, используя то же соединение.
Попробуйте insert
использовать второй курсор:
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()