WebscrapperPython/Stadt_Rubrik.py

44 lines
1.2 KiB
Python

import mysql.connector
import time
def connect_db():
return mysql.connector.connect(
host="192.168.178.201", # Ändere dies nach Bedarf
user="gelbeseiten",
password="Gm4bBE62gXCSVVY2",
database="domainchecker",
connection_timeout=300
)
# Verbindung herstellen
db = connect_db()
cursor = db.cursor()
try:
cursor.execute("SELECT staedte FROM staedte")
staedte = cursor.fetchall()
cursor.execute("SELECT rubriken FROM rubriken")
rubriken = cursor.fetchall()
data = [(stadt[0], rubrik[0], 0) for stadt in staedte for rubrik in rubriken]
batch_size = 1000
for i in range(0, len(data), batch_size):
try:
cursor.executemany("INSERT INTO staedte_rubriken (stadt, rubrik, status) VALUES (%s, %s, %s)", data[i:i+batch_size])
db.commit()
print(f"{i+batch_size} Datensätze eingefügt...")
except mysql.connector.errors.OperationalError:
print("Verbindung verloren, versuche erneut zu verbinden...")
time.sleep(5) # Warten und neu versuchen
db = connect_db()
cursor = db.cursor()
except mysql.connector.Error as err:
print(f"Fehler: {err}")
finally:
cursor.close()
db.close()