当前位置: 首页 > news >正文

汕尾网站设计公司网站程序

汕尾网站设计,公司网站程序,vip影视网站怎么做的,大良营销网站建设讯息获取不同类型的数据源: 对于看上的网站如何获取其信息: 1.分析原网站是如何获取到这些数据的?哪个接口?哪些参数? 2.用程序去调用接口(python/java都可以) 3.处理一些数据,优化数…

获取不同类型的数据源:

对于看上的网站如何获取其信息:

1.分析原网站是如何获取到这些数据的?哪个接口?哪些参数?

2.用程序去调用接口(python/java都可以)

3.处理一些数据,优化数据传入数据库

java爬虫操作流程:

先创建一个实体类:根据网络上需要操作的请求的属性规定实体类属性一一对应。

爬取:https://www.code-nav.cn/learn/passage

数据抓取的几种方式:

1.直接去请求接口(最方便)HttpClient,OkHttp,Hutool,resttemplate

2.等网页渲染出明文内容后,从前端页面的内存抓取

3.有一些网站可能是动态请求的,他不会一次性加载所有数据,而是要你点击某个按钮,输入某个验证码后才会显示出数据。 => 无头浏览器(后台代替开启浏览器)比如:java的selenium和nodejs的puppeteer

数据抓取流程:

1.分析数据源(怎么获取)

2.拿到数据后,怎么处理?

3.写入数据库等存储

1.方式一:使用okhttp

        1.引入依赖

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.1.0</version>
</dependency>

        2.构造请求

Okhttp3 完成页面请求,需要三大步骤:

  1. 实例化 OkHttpClient
  2. 执行调用。
    1. 在执行调用之前,需要实例化一个 Request 对象
    2. 然后构建调用对象 
    3. 最后执行调用,如果调用失败可能抛异常,所以必须抓取异常
  3. 调用对象的方法即可获取返回的字符串内容

get请求

public class Main {public static void main(String[] args) throws IOException {String url = "https://4399.com";OkHttpClient okHttpClient = new OkHttpClient();Request request = new Request.Builder().url(url).build();Call call = okHttpClient.newCall(request);String string = call.execute().body().string();System.out.println(string);}
}

 post请求

    public static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");/*** 向指定的 url 提交数据,以 json 的方式*/public String postContent(String url, Map<String, String> datas) {// okHttpClient 实例OkHttpClient okHttpClient = new OkHttpClient();// 数据对象转换成 json 格式字符串String param = JSON.toJSONString(datas);//post方式提交的数据RequestBody requestBody = RequestBody.create(JSON_TYPE, param);Request request = new Request.Builder().url(url).post(requestBody).build();// 使用client去请求Call call = okHttpClient.newCall(request);// 返回结果字符串String result = null;try {// 获得返回结果result = call.execute().body().string();} catch (IOException e) {// 抓取异常System.out.println("request " + url + " error . ");e.printStackTrace();}return result;}public static void main(String[] args) {String url = "https://4399.com";Map<String, String> datas = new HashMap();datas.put("num", "6666");Main poster = new Main();String content = poster.postContent(url, datas);System.out.println("API调用结果");System.out.println(content);}

2.方式二:使用Hutool

<!-- https://hutool.cn/docs/index.html#/-->
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.8</version>
</dependency>
public static void main(String[] args) throws IOException {String json = "{\n" +"  \"current\": 1,\n" +"  \"pageSize\": 8,\n" +"  \"sortField\": \"createTime\",\n" +"  \"sortOrder\": \"descend\",\n" +"  \"category\": \"文章\",\n" +"  \"tags\": [],\n" +"  \"reviewStatus\": 1\n" +"}";String url = "https://4399.com";String result2 = HttpRequest.post(url).body(json).execute().body();String uu = "F:\\user-center-backend\\src\\main\\java\\com\\yupi\\usercenter";File file = new File(uu, "result.json");file.createNewFile();FileWriter fileWriter = new FileWriter(file);fileWriter.write(result2);System.out.println(result2);
}

3.方式三:使用Jsoup

        1.引入依赖

<!--        jsoup--><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.15.3</version></dependency>

        2.构造请求 

package org.example.cetidenet;import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.cetidenet.model.entity.Post;
import org.example.cetidenet.service.PostService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;@SpringBootTest
class CetideNetApplicationTests {@Autowiredprivate PostService postService;void testFetchPic() throws IOException {int current = 1;String url = "https://4399.com";Document doc = Jsoup.connect(url).get();Elements elements = doc.select(".iuscp.isv");for(Element h : elements){//取图片地址(murl)String m = h.select(".iusc").get(0).attr("m");//取地址Map<String,Object> result = JSONUtil.toBean(m,Map.class);String murl = (String)result.get("murl");System.out.println(murl);String title =h.select(".inflnk").get(0).attr("aria-label");System.out.println(title);}}
}

http://www.yayakq.cn/news/710104/

相关文章:

  • 网站空格 教程粉末涂料 技术支持 东莞网站建设
  • 灵璧做网站公司怎么制作软件app教程
  • 网站建设结项报告网站需要改进的地方
  • 做百度网站那家好网页设计商城网站建设
  • 自建网站步骤网站开发建设技术规范书
  • 织梦网站内容管理系统做网站的公司怎么样
  • 昆山营销型网站建设做资源分享网站
  • 好用的h5网站模板下载网站后台管理js
  • 大连网站建设报价优质商家社交网站的建设现状
  • html好看的网站的代码跨境电商产品开发流程
  • dede网站头部不显示调用的名称网站开发命名规范
  • 网站 后台 设计个人网站备案后做游戏
  • 现在做网络推广网站建设怎么样无锡企业网络推广服务
  • 网站开发国内外研究状况什么软件可以做app软件
  • 网站开发模式太原做企业网站
  • phpstudy网站建设教程做网站3个月
  • 乐山做网站做医疗科普的网站
  • 网站建设公司宣传词汕头珠宝网站建设
  • 天津做网站优化公司wordpress 相册 样式
  • 网站备案后 还是需要再备案吗玉环做网站有哪些
  • 1g1m wordpress网站优化推广公司推荐
  • 猫咪网站模版下载计算机网站开发是那个语言
  • wordpress做一个网站404引导开发一个公众号需要多少钱
  • 深圳企业网站建设推荐公司购物网站开发和运行环境
  • 同主机网站查询wordpress加载单页面
  • 公司专业设计网站轻量的wordpress
  • 做网站有了空间在备案吗誓做中国最大钓鱼网站
  • 一流的菏泽网站建设网站开发要用cms
  • 朋友圈网站文章怎么做赣州同城网
  • 高性能网站开发北京到广州快递要几天