https://github.com/lixelv/kcal-parse
https://github.com/lixelv/kcal-parse
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lixelv/kcal-parse
- Owner: lixelv
- Created: 2023-08-11T19:04:54.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-18T19:22:25.000Z (over 2 years ago)
- Last Synced: 2025-02-23T20:52:06.159Z (over 1 year ago)
- Language: Python
- Size: 773 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
db.py:
```py
import sqlite3
class DB:
def __init__(self, db_name):
self.connect = sqlite3.connect(db_name)
self.cursor = self.connect.cursor()
def do(self, sql, values=()) -> None:
self.cursor.execute(sql, values)
self.connect.commit()
def read(self, sql, values=()) -> tuple:
self.cursor.execute(sql, values)
return self.cursor.fetchall()
def add_second_name(self, values: list | tuple | set) -> None:
self.do('INSERT INTO second_name (name, url, first_name_id) VALUES (?, ?, ?)', tuple(values))
def add_data(self, values: list | tuple | set) -> None:
self.do(f'INSERT INTO data (product, kcal, protein, fat, carbonates, url, second_name_id) VALUES (?, ?, ?, ?, ?, ?, ?)', tuple(values))
```
main.py:
```py
import requests
from bs4 import BeautifulSoup
from db import DB
sql = DB('db.db')
l = sql.read('SELECT id, url FROM second_name')
for id, url in l:
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
soup = soup.find('table', class_='uk-table mzr-tc-group-table uk-table-hover uk-table-striped uk-table-condensed').find('tbody')
for i in soup.find_all('tr'):
j = i.find_all('td')
result = [j[0].get_text()[:-2]]+[float(x.get_text().split(' ')[0].replace(',', '.')) for x in j[1:]]+[f"https://health-diet.ru{j[0].find('a').get('href')}", id]
print(result)
sql.add_data(result)
```
test1.py:
```py
from bs4 import BeautifulSoup
html = """
Элемент 1
Элемент 2
Элемент 3
Элемент 4
Элемент 5
Элемент 6
"""
soup = BeautifulSoup(html, 'html.parser')
# Находим все элементы с классом "example"
elements = soup.find_all(class_="example")
# Получаем первый элемент
first_element = elements[0]
print(first_element.text) # Вывод: Элемент 1
# Получаем шестой элемент
sixth_element = elements[5]
print(sixth_element.text) # Вывод: Элемент 6
```
test2.py:
```py
```