公司手机网站效果图,网站被抄袭怎么投诉,动漫在线设计平台,查做外贸客户的网站获取不同类型的数据源#xff1a;
对于看上的网站如何获取其信息#xff1a;
1.分析原网站是如何获取到这些数据的#xff1f;哪个接口#xff1f;哪些参数#xff1f;
2.用程序去调用接口#xff08;python/java都可以#xff09;
3.处理一些数据#xff0c;优化数…获取不同类型的数据源
对于看上的网站如何获取其信息
1.分析原网站是如何获取到这些数据的哪个接口哪些参数
2.用程序去调用接口python/java都可以
3.处理一些数据优化数据传入数据库 java爬虫操作流程
先创建一个实体类根据网络上需要操作的请求的属性规定实体类属性一一对应。 爬取https://www.code-nav.cn/learn/passage 数据抓取的几种方式
1.直接去请求接口最方便HttpClientOkHttpHutoolresttemplate
2.等网页渲染出明文内容后从前端页面的内存抓取
3.有一些网站可能是动态请求的他不会一次性加载所有数据而是要你点击某个按钮输入某个验证码后才会显示出数据。 无头浏览器(后台代替开启浏览器)比如java的selenium和nodejs的puppeteer
数据抓取流程
1.分析数据源怎么获取
2.拿到数据后怎么处理
3.写入数据库等存储 1.方式一使用okhttp 1.引入依赖
!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --
dependencygroupIdcom.squareup.okhttp3/groupIdartifactIdokhttp/artifactIdversion4.1.0/version
/dependency 2.构造请求
Okhttp3 完成页面请求需要三大步骤
实例化 OkHttpClient执行调用。 在执行调用之前需要实例化一个 Request 对象然后构建调用对象 最后执行调用如果调用失败可能抛异常所以必须抓取异常调用对象的方法即可获取返回的字符串内容
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; charsetutf-8);/*** 向指定的 url 提交数据以 json 的方式*/public String postContent(String url, MapString, 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;MapString, 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#/--
dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.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--dependencygroupIdorg.jsoup/groupIdartifactIdjsoup/artifactIdversion1.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);//取地址MapString,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);}}
}