I created a model inside my django project. I also have a python file with my function. I want to display the function results in the database(mysql) table inside the model file I created. I imported the model in the file where my function is located. What do I need to do to fix this problem?
Codes of the file with the function:
from ast import Str
from base64 import decode
import requests
from bs4 import BeautifulSoup
import pandas as pd
import csv
from time import sleep
from random import randint
import numpy as np
from bookList.models import BookList
headers = dict()
headers[
"User-Agent"
] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
def sendBooksToMysql():
bkmArt()
def bkmArt():
pages = range(1, 3, 1)
for page in pages:
url = "https://www.bkmkitap.com/sanat"
results = requests.get(url, headers=headers)
soup = BeautifulSoup(results.content, "html.parser")
book_div = soup.find_all("div", class_="col col-12 drop-down hover lightBg")
sleep(randint(2, 10))
for bookSection in book_div:
img_url = bookSection.find("img", class_="lazy stImage").get('data-src')
name = bookSection.find("a",class_="fl col-12 text-description detailLink").get('title')
author = bookSection.find("a", class_="fl col-12 text-title").get('title').replace('Model:', '')
publisher = bookSection.find("a", class_="col col-12 text-title mt").get('title').replace(name, '')
price = bookSection.find("div", class_="col col-12 currentPrice").text.replace('\n', '').replace('TL', ' TL')
b_link = bookSection.find("a").get('href')
bookList = BookList.objects.using("bookList").update_or_create(
site_name="Bkmkitap",
site_id="1",
image=img_url,
#book=name,
#author=author,
publisher=publisher,
price=price,
link=b_link,
category_name="Sanat",
category_id="1",
defaults={
"site_name":"Bkmkitap",
"site_id":"1",
"image":img_url,
"book":name,
"author":author,
"publisher":publisher,
"price":price,
"link":b_link,
"category_name":"Sanat",
"category_id":"1",
}
)
models.py file:
from django.db import models
# Create your models here.
class BookList(models.Model):
site_name = models.TextField()
site_id = models.TextField()
image = models.TextField()
book = models.TextField()
author = models.TextField()
publisher = models.TextField()
price = models.TextField()
link = models.TextField()
category_name = models.TextField()
category_id = models.TextField()
class Meta:
managed = True
db_table = 'books'