2 Commits

Author SHA1 Message Date
laktionov-as 9f05f0a9f9 добавил from_dict 2026-06-10 21:06:37 +03:00
laktionov-as 0bd384b252 Итоговый проект 2026-06-10 20:48:42 +03:00
2 changed files with 39 additions and 73 deletions
BIN
View File
Binary file not shown.
+31 -65
View File
@@ -1,79 +1,45 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import re import re
import sqlite3
from datetime import datetime
class Spyder: url = 'https://msk.top-academy.ru/blog'
def __init__(self):
self.db_name = 'database.db' class BlogArticle:
self.conn = sqlite3.connect(self.db_name) ls = list()
self.cursor = self.conn.cursor() def __init__(self,title,text):
def init_db(self): self.title = title
query = '''CREATE TABLE links( self.text = text
id INTEGER PRIMARY KEY AUTOINCREMENT, BlogArticle.ls.append(self)
link TEXT NOT NULL, @classmethod
last_scan INTEGER DEFAULT 0, def count(cls):
title TEXT DEFAULT '' return len(cls.ls)
)'''
self.cursor.execute(query) def to_dict(self):
def scan(self,url): return {'title': self.title, 'text':self.text}
if url.endswith('.pdf'):
return @classmethod
def from_dict(cls,d):
BlogArticle(d['title'],d['text'])
def scan(i):
headers = { headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36' 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36'
} }
try:
response = requests.get( response = requests.get(
url, url + (f'?page={i}' if i>1 else ''),
headers=headers, headers=headers
timeout=5
) )
except:
self.update_url(url,'Scan error')
return
html = response.text html = response.text
soup = BeautifulSoup(html,'html.parser') soup = BeautifulSoup(html,'html.parser')
result = soup.find_all("a", href=True) result = soup.find_all("div", class_='styles_cardBody__qP0jN')
title = soup.title.text
self.update_url(url,title)
for el in result: for el in result:
if el['href'].endswith('.pdf'): par = el.find_all("p")
continue BlogArticle(par[0].text,par[1].text)
if not el['href'].startswith('http'):
match = re.findall(r'https?://[A-Za-z0-9.-]+',url)
link = f"{match[0]}{el['href']}"
else:
link = el['href']
query = f'SELECT id FROM links WHERE link = ?'
self.cursor.execute(query,(link,))
if self.cursor.fetchone():
continue
query = f'INSERT INTO links(link) VALUES(?)'
self.cursor.execute(query,(link,))
self.conn.commit()
def update_url(self,url,title):
query = "SELECT id FROM links WHERE link = ?"
self.cursor.execute(query,(url,))
link_id = self.cursor.fetchone()
if link_id:
query = "UPDATE links SET title=?, last_scan=? WHERE id=?"
self.cursor.execute(query,(title,datetime.now().timestamp(),link_id[0]))
self.conn.commit()
def get_url(self):
query = "SELECT link FROM links ORDER BY last_scan LIMIT 1"
self.cursor.execute(query)
return self.cursor.fetchone()
def close(self):
self.conn.close()
if __name__ == '__main__': if __name__ == '__main__':
spyder = Spyder() BlogArticle.from_dict({'title':'Заголовок','text':'Текст статьи'})
#spyder.init_db() #for i in range(3):
#spyder.scan('https://habr.com/ru/news/1046177/') #scan(i)
for _ in range(5): print(BlogArticle.count())
url = spyder.get_url() for i in range(1):
print(url[0]) print(BlogArticle.ls[i].to_dict())
spyder.scan(url[0])
spyder.close()