我打算做个性化新闻推荐的原生app,需要获取新闻文本并计算关键字来进行推荐。目前遇到的困难是不太了解网站上的图文新闻应该如何爬取并显示在客户端上?我目前只了解纯文本信息的爬取显示,但是对于夹带图片的新闻应该如何保存并在浏览时保持排版呢,数据库里应该如何存储
参考GPT和自己的思路:
首先,获取图文新闻的方法可以应用网络爬虫技术,爬取新闻网站上的HTML页面,解析其中的图文信息并保存。一般来说,这些图文信息包括HTML标记、CSS或JavaScript等脚本文件,以及图片或视频等多媒体内容。
对于保存夹带图片的新闻,你可以将新闻文本和图片等资源保存到一个文件夹中,然后在客户端上显示时,将相关图片链接插入到HTML页面中。为了让图文排版更美观,你可以考虑使用CSS样式表来定义排版规则,如字体、字号、间距等。
关于数据存储,你可以使用数据库来存储新闻文本、图像、时间戳和其他相关信息。你可以选择关系型数据库如MySQL或非关系型数据库如MongoDB。具体的数据库设计取决于你的应用需求和性能要求。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
获取新闻图文数据通常需要分为两个步骤:首先是爬取新闻数据,其次是在客户端中显示新闻数据。
在获取新闻图片时,你可以使用Python中的第三方库(如Pillow或OpenCV)来处理图片数据。获取到图片后,你需要将其存储到本地磁盘上,同时记录下图片的路径,以便在客户端中正确显示图片。
在存储新闻数据到数据库时,你需要将新闻标题、正文、图片路径等信息存储到不同的表中,并在表之间建立关联关系。例如,可以将新闻正文存储到一个单独的表中,然后将该表的ID作为外键存储到包含新闻标题和图片路径的表中。这样,当你需要获取某个新闻的数据时,就可以通过外键关系将所有相关数据查询出来。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewsDetailActivity"
android:orientation="vertical">
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/wv_news"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/edt_comment"
android:hint="请文明发言"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="评论"
android:id="@+id/btn_comment"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@drawable/fav_off"
android:id="@+id/iv_fav"/>
</LinearLayout>
</LinearLayout>