xinhua_base
This commit is contained in:
23
core/management/commands/crawl_xinhua.py
Normal file
23
core/management/commands/crawl_xinhua.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import crawl_xinhua_article
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '爬取新华网文章示例'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# 假设你事先在后台建了“新华网”这个Website实例
|
||||
website_name = "新华网"
|
||||
try:
|
||||
website = Website.objects.get(name=website_name)
|
||||
except Website.DoesNotExist:
|
||||
self.stdout.write(self.style.ERROR(f"网站 '{website_name}' 不存在,请先后台创建"))
|
||||
return
|
||||
|
||||
# 这里写你想爬取的文章URL列表,可以循环多篇
|
||||
urls = [
|
||||
"https://www.news.cn/legal/20250721/f340f7be3d5b4b938cbd6b9889b6fbdc/c.html",
|
||||
]
|
||||
|
||||
for url in urls:
|
||||
crawl_xinhua_article(url, website)
|
||||
Reference in New Issue
Block a user