From ab06287ca19b9937369f55a746c8a531969030c8 Mon Sep 17 00:00:00 2001 From: BlackTea-c <2286554510@qq.com> Date: Sat, 9 Mar 2024 11:49:30 +0800 Subject: [PATCH] 114514 --- crawler/crawler.py | 49 ++++++----- moiveRe/import_data.py | 2 +- moiveRe/moiveReApp/models.py | 6 +- .../templates/moiveReApp/index.html | 87 ++++++++++++++++--- .../templates/moiveReApp/ratings.html | 12 ++- moiveRe/moiveReApp/urls.py | 3 +- moiveRe/moiveReApp/views.py | 26 ++++++ 7 files changed, 144 insertions(+), 41 deletions(-) diff --git a/crawler/crawler.py b/crawler/crawler.py index d234574..35e8867 100644 --- a/crawler/crawler.py +++ b/crawler/crawler.py @@ -1,30 +1,32 @@ import os import sys -import django from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from bs4 import BeautifulSoup -from datetime import datetime +import csv + +data_list = [] -data_list=[] def scrape_mod_pages(url, num_pages): - driver = webdriver.Chrome() # 请确保你已经安装了Chrome浏览器,并配置好webdriver + driver = webdriver.Chrome() try: + current_url = url # 保存当前URL + for page in range(1, num_pages + 1): - # 访问当前页 - driver.get(url) + driver.get(current_url) # 使用保存的URL + + # 确认当前页面是否是目标页面 + confirmation = input("确认当前页面是否是你想爬取的页面?(y/n): ") + if confirmation.lower() != 'y': + break - # 使用WebDriverWait等待页面元素加载完成 WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'mod-tile'))) - # 获取网页源代码 page_source = driver.page_source - - # 使用BeautifulSoup解析HTML soup = BeautifulSoup(page_source, 'html.parser') mod_tiles = soup.find_all('li', class_='mod-tile') @@ -42,26 +44,25 @@ def scrape_mod_pages(url, num_pages): print(f"Category: {category}") print(f"Image URL: {img_url}") print("\n") - # 将爬取到的数据存储到列表中 - data_list.append([title, link, description, category, img_url]) + data_list.append([title, link, description, category, img_url]) - # 等待用户手动切换页面,这里我真没找到比较好的方法,大一大二自学的爬虫到大四直接忘了真是艹了 - #(所以说去github之类的网站记录东西真的很有必要= =) - input("Please switch to the next page manually and press Enter when ready...") - #设置num_pages,是多少就会循环多少次。 - + # 询问用户是否切换到下一页 + next_page = input("是否切换到下一页?(y/n): ") + if next_page.lower() == 'y': + current_url = driver.current_url # 保存当前页面的URL + else: + break finally: - # 关闭浏览器 driver.quit() -# 调用爬虫程序,例如爬取前5页的内容 + url_to_scrape = 'https://www.nexusmods.com/skyrimspecialedition/mods/' -scrape_mod_pages(url_to_scrape, num_pages=5) -# 将数据写入 CSV 文件 -import csv -with open('../moiveRe/mod_data.csv', 'w', newline='', encoding='utf-8') as csv_file: +scrape_mod_pages(url_to_scrape, num_pages=8) + +# 将数据写入CSV文件 +with open('mod_data1.csv', 'w', newline='', encoding='utf-8') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow(['Title', 'Link', 'Description', 'Category', 'Image URL']) # 写入表头 - csv_writer.writerows(data_list) \ No newline at end of file + csv_writer.writerows(data_list) diff --git a/moiveRe/import_data.py b/moiveRe/import_data.py index b80e94f..dcc407d 100644 --- a/moiveRe/import_data.py +++ b/moiveRe/import_data.py @@ -14,7 +14,7 @@ from datetime import datetime from django.db import models from moiveReApp.models import Question -csv_file_path = 'mod_data.csv' +csv_file_path = 'mod_data1.csv' questions_to_create = [] with open(csv_file_path, mode='r', encoding='utf-8') as csv_file: csv_reader = csv.DictReader(csv_file) diff --git a/moiveRe/moiveReApp/models.py b/moiveRe/moiveReApp/models.py index 971bf6e..29adfd0 100644 --- a/moiveRe/moiveReApp/models.py +++ b/moiveRe/moiveReApp/models.py @@ -6,6 +6,7 @@ import datetime from django.utils import timezone from django.core.validators import MinValueValidator, MaxValueValidator +from django.contrib.auth.models import User class Tag(models.Model): name = models.CharField(max_length=200) @@ -23,6 +24,9 @@ class Question(models.Model): link = models.CharField(max_length=200,default='http://127.0.0.1:8000/moiveReApp/') detail = models.CharField(max_length=600,default=' ') pub_date = models.DateTimeField("date published") + + likes = models.PositiveIntegerField(default=0) + liked_by = models.ManyToManyField(User, blank=True, related_name='liked_questions') def __str__(self): return f"{self.question_text} - {self.img.url}" @@ -47,8 +51,6 @@ def avg_rating(self): return None - - class Rating(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) user = models.CharField(max_length=255,default='some_default_value') diff --git a/moiveRe/moiveReApp/templates/moiveReApp/index.html b/moiveRe/moiveReApp/templates/moiveReApp/index.html index f3f8754..6cecd91 100644 --- a/moiveRe/moiveReApp/templates/moiveReApp/index.html +++ b/moiveRe/moiveReApp/templates/moiveReApp/index.html @@ -57,6 +57,7 @@ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); overflow: hidden; transition: transform 0.3s ease; + position: relative; } li:hover { @@ -86,36 +87,36 @@ overflow: hidden; font-size: 18px; font-weight: bold; - color: #fff; /* 设置question_text为白色 */ + color: #fff; } p.tags { margin: 0; padding: 5px; - font-size: 12px; /* 调小标签文字大小 */ + font-size: 12px; display: flex; justify-content: center; align-items: center; } span.tag-item { - background-color: #3498db; /* 标签框填充颜色 */ - color: #fff; /* 白色文字 */ - border-radius: 5px; /* 圆角框 */ - padding: 3px 8px; /* 调整内边距 */ - margin: 2px; /* 调整外边距 */ - box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */ + background-color: #3498db; + color: #fff; + border-radius: 5px; + padding: 3px 8px; + margin: 2px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } p.detail { margin: 0; padding: 5px; display: -webkit-box; - -webkit-line-clamp: 3; /* 设置显示的最大行数 */ + -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; - font-size: 8px; /* 进一步调小detail的字体大小 */ - color: #fff; /* 设置detail为白色 */ + font-size: 8px; + color: #fff; } .rating { @@ -137,6 +138,45 @@ font-size: 12px; color: #95a5a6; } + + .like-button { + position: absolute; + bottom: 10px; + right: 10px; + cursor: pointer; + color: #f39c12; + font-size: 24px; + } + + .like-button i { + transition: color 0.3s, transform 0.5s; + } + + .like-button.liked i { + color: #ff6b6b; + transform: scale(1.5); + } + + .firework { + position: absolute; + width: 5px; + height: 5px; + background-color: #ff6b6b; + border-radius: 50%; + animation: explode 0.5s ease-out; + transform: translate(60%, -150%); + } + + @keyframes explode { + 0% { + transform: scale(1); + opacity: 1; + } + 100% { + transform: scale(1); + opacity: 0; + } + } @@ -146,7 +186,6 @@
发布日期: {{ question.pub_date|date:"F d, Y" }}
+ + {% endfor %} + +