在抓取后将简单列插入数据库时出现问题



我无法在数据库中保存一个简单列。刮削是可以的。打印输出正常。刮取打印后,会立即显示一条错误消息,并且数据库条目失败。事实上,所有这些名称都应该插入数据库的(垂直(列中。仅一(1(列。保存在数据库中有什么错误?

driver.get("link 1")
driver.close
Example1=driver.find_element_by_class_name("teamHeader__name")
print(Example1.text)
driver.get("link 2")
driver.close
Example2=driver.find_element_by_class_name("teamHeader__name")
print(Example2.text)
driver.get("link 3")
driver.close
Example3=driver.find_element_by_class_name("teamHeader__name")
print(Example3.text)

#INSERT IN DATABASE
con = sqlite3.connect('/home/mypc/Desktop/aaaaaaaa/Database.db')
cursor = con.cursor()
records_added_Risultati = 0
Values = (Example1.text, Example2.text, Example3.text)
sqlite_insert_query = 'INSERT INTO TableExample (AllExamples) VALUES (?);'
count = cursor.execute(sqlite_insert_query, Values)
con.commit()
print("Record inserted successfully ", cursor.rowcount)
records_added_Risultati = records_added_Risultati + 1
cursor.close()

错误:

Traceback (most recent call last):
File "/home/mypc/Desktop/aaaaaaaa/Progetto.py", line 192, in <module>
Values = (Example1.text, Example2.text, Example3.text)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 76, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/home/mypc/.local/lib/python3.8/site-packages    /selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException:    Message: The element reference of [object String] "{"element-6066-11e4-a52e-4f735466cecf":"ddc55fe3-1fe4-4560- bcf5-62d18fbeae63"}" is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

上传1`

刮削是可以的。打印输出正常。

#Serie A
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieA=driver.find_element_by_class_name("teamHeader__name")
SerieA_text = SerieA.text
print(SerieA.text)
#Serie B
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieB=driver.find_element_by_class_name("teamHeader__name")
SerieB_text = SerieB.text
print(SerieB.text)
#Serie C - Gir A
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieC_A=driver.find_element_by_class_name("teamHeader__name")
SerieC_A_text = SerieC_A.text
print(SerieC_A.text)
and 8 others the same ... 
### INSERT IN DATABASE ###
con = sqlite3.connect('/home/mypc/Scrivania/aaaaa/Database.db')
cursor = con.cursor()
records_added_Risultati = 0
Values = ((SerieA_text), (SerieB_text), (SerieC_A_text), (SerieC_B_text), (SerieC_C_text), (SerieD_I_text), (SerieD_H_text), (PremierLeague_text), (Championship_text), (Liga_text), (Bundesliga_text), (Ligue1_text))
sqlite_insert_query = 'INSERT INTO ARCHIVIO_Campionati (Nome_Campionato) VALUES (?);'
count = cursor.executemany(sqlite_insert_query, Values) #executemany, no execute
con.commit()
print("Record inserted successfully ", cursor.rowcount)
records_added_Risultati = records_added_Risultati + 1
cursor.close()

driver.get将加载一个新页面。在代码中,您将加载3个页面,并从所有这些页面中获取元素。一旦加载了link 2link 1页面中的Example1元素就会过时。你可以做的是,一旦你得到元素,你也可以在一个变量中得到它的文本。并且,对下一个链接再次执行相同的操作。

driver.get("link 1")
driver.close
Example1=driver.find_element_by_class_name("teamHeader__name")
example1_text = Example1.text
print(Example1.text)
driver.get("link 2")
driver.close
Example2=driver.find_element_by_class_name("teamHeader__name")
example2_text = Example2.text
print(Example2.text)
driver.get("link 3")
driver.close
Example3=driver.find_element_by_class_name("teamHeader__name")
example3_text = Example3.text
print(Example3.text)

稍后,使用存储文本的变量,而不是Values = (Example1.text, Example2.text, Example3.text)

Values = ((example1_text,), (example2_text,), (example3_text,))

还请注意,Values中的每个项目都是具有列值的单行。这就是为什么这里的每个项都是一个元组。此外,您应该使用cursor.executemany,因为您要一次性插入多个值。

最新更新