Skip to content

Commit

Permalink
114514
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackTea-c committed Mar 9, 2024
1 parent cce622b commit ab06287
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 41 deletions.
49 changes: 25 additions & 24 deletions crawler/crawler.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
csv_writer.writerows(data_list)
2 changes: 1 addition & 1 deletion moiveRe/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions moiveRe/moiveReApp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}"

Expand All @@ -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')
Expand Down
87 changes: 75 additions & 12 deletions moiveRe/moiveReApp/templates/moiveReApp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
</style>
</head>

Expand All @@ -146,7 +186,6 @@ <h1>Skyrim Mod</h1>
{% if user.is_authenticated %}
<div class="user-info">Welcome, {{ user.username }}!</div>
{% endif %}
<!-- 添加排序链接和图标 -->
<div class="sort-link">
<a href="{% url 'moiveReApp:random_questions' %}">
<i class="fas fa-random"></i> Random Sort
Expand All @@ -170,10 +209,34 @@ <h1>Skyrim Mod</h1>
{{ question.avg_rating|default:5|floatformat:1 }}/5
</div>
<p class="pub-date">发布日期: {{ question.pub_date|date:"F d, Y" }}</p>

<form action="{% url 'moiveReApp:like_question' question.id %}" method="post">
{% csrf_token %}
<button type="submit" class="like-button">
{% if user.is_authenticated and user in question.liked_by.all %}
<i class="fas fa-heart liked"></i>
{% else %}
<i class="far fa-heart"></i>
{% endif %}
<div class="firework"></div>
</button>
</form>
</a>
</li>
{% endfor %}
</ul>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
$('.like-button').click(function () {
var fireworks = $('<div class="firework"></div>');
$(this).append(fireworks);
setTimeout(() => {
fireworks.remove();
}, 500);
});
});
</script>
</body>

</html>
12 changes: 11 additions & 1 deletion moiveRe/moiveReApp/templates/moiveReApp/ratings.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
margin-bottom: 20px;
}

h1 a {
color: #61dafb;
text-decoration: none;
font-size: 18px;
}

h1 a:hover {
color: #4fa3d1;
}

img {
max-width: 100%;
height: auto;
Expand Down Expand Up @@ -79,7 +89,7 @@
<body>
<div class="container">
<img src="{% if question.img_url %}{{ question.img_url }}{% else %}{{ question.img.url }}{% endif %}" alt="Image">
<h1>{{ question.question_text }}</h1>
<h1><a href="{{ question.link }}" target="_blank">查看详情</a></h1>

<h2>我要评分</h2>
<form method="post" action="{% url 'moiveReApp:question_detail' question_id=question.id %}">
Expand Down
3 changes: 2 additions & 1 deletion moiveRe/moiveReApp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from django.urls import include, path
from django.views.static import serve
from .views import question_detail
from .views import register, user_login,random_questions
from .views import register, user_login,random_questions,like_question

app_name = "moiveReApp"
urlpatterns = [
path("", views.IndexView.as_view(), name="index"),
path('register/', register, name='register'),
path('login/', user_login, name='user_login'),
path('randomsort/', random_questions, name='random_questions'),
path('like/<int:question_id>/', like_question, name='like_question'),

path('<int:question_id>/ratings/', question_detail, name='question_detail'),

Expand Down
26 changes: 26 additions & 0 deletions moiveRe/moiveReApp/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from django.views import generic
Expand All @@ -7,6 +8,8 @@
from django.shortcuts import render, get_object_or_404, redirect
from .models import Question, Rating
import random
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required


class IndexView(generic.ListView):
Expand Down Expand Up @@ -73,4 +76,27 @@ def random_questions(request):
return render(request, 'moiveReApp/randomsort.html', {'random_questions': random_questions})


@login_required
def like_question(request, question_id):
question = get_object_or_404(Question, id=question_id)

if request.user in question.liked_by.all():
# 用户已经点过赞,取消点赞
question.liked_by.remove(request.user)
if question.likes>0:
question.likes-=1
else:
# 用户还未点赞,添加点赞
question.liked_by.add(request.user)
question.likes += 1

question.save()

# 获取请求的来源URL,如果没有则默认重定向到首页
redirect_url = request.META.get('HTTP_REFERER', 'moiveReApp:index')

# 重定向到来源页面
return redirect(redirect_url)



0 comments on commit ab06287

Please sign in to comment.