I have a python file in my Django project which scrapes 10 names from a website. I want to store these 10 names in a postgresql database.
Below is the python file.
import requests
import urllib3
from bs4 import BeautifulSoup
import psycopg2
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = requests.Session()
session.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"}
url = 'https://www.smitegame.com/'
content = session.get(url, verify=False).content
soup = BeautifulSoup(content, "html.parser")
allgods = soup.find_all('div', {'class': 'god'})
allitem = []
for god in allgods:
godName = god.find('p')
godFoto = god.find('img').get('src')
allitem.append((godName, godFoto))
print(godName.text)
How do I need to approach this, I've made a class in models.py named GodList. But as soon as I try to import it I cannot run the scrape script anymore.
Am I aproaching this wrong?
I have the postgresql database connected to Django and it works. I can add models and I see it gets saved in the data base.