HttpUtils
老教程已经进不去京东了,因为直接爬取会被拦截到京东登录页.
我们需要进行请求头伪装.
1. 到搜索页面F12 -----> network---->获取信息复制
2. 到https://curlconverter.com/java/ 直接转成HttpGet设置代码
package cn.itcast.jd.util;
...
引导类
package cn.itcast.jd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.schedu...
POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi...
地址: itcast-crawler-first.zip
5.2.7. Selector选择器组合使用
el#id: 元素+ID,比如: h3#city_bj
el.class: 元素+class,比如: li.class_a
el[attr]: 元素+属性名,比如: span[abc]
任意组合: 比如:span[abc].s_name
ancestor child: 查找某个元素下子元素,比如:.city_c...
Selector选择器概述
tagname: 通过标签查找元素,比如:span
id: 通过ID查找元素,比如:# city_bj
.class: 通过class名称查找元素,比如:.class_a
测试类文件
@Test
public void testSelector()throws Exception{
//解析html...
元素中获取数据
从元素中获取id
从元素中获取className
从元素中获取属性的值attr
从元素中获取所有属性attributes
从元素中获取文本内容text
测试类文件
@Test
public void testData()throws Exception{
//解析文件,获取document
Docu...
使用dom方式遍历文档
元素获取
根据id查询元素getElementById
根据标签获取元素getElementsByTag
根据class获取元素getElementsByClass
根据属性获取元素getElementsByAttribute
测试类文件
@Test
public void testDOM()throws Except...
Test测试类文件
@Test
public void testFile()throws Exception{
//解析文件
Document doc = Jsoup.parse(new File("C:\\Users\\16259\\Desktop\\test.html"),"utf8");
Str...
测试类文件
@Test
public void testString()throws Exception{
//使用工具类获取字符串
String content = FileUtils.readFileToString(new File("C:\\Users\\16259\\Desktop\\test.html")...
类文件
HttpClient适合于抓取数据,Jsoup适合解析数据,所以都要学习.
package jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;
import java.net.URL;
public class JsoupF...
类文件
可以给请求设置多种参数,例如创建连接的最长时间,获取连接的最长时间,设置数据传输的最长时间
package cn.itcast.crawler.test;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.hc.client5.http.classic.methods.Htt...
类文件
注意由于交给连接池管理HttpClient了,所以我们在doGet方法中不要自己关闭HttpClient
package cn.itcast.crawler.test;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.ht...
原因: 使用httpClient访问时,地址忘了加 http:// 协议了
解决方法: 在网址前加上http://
引用 https://blog.csdn.net/Imobama/article/details/92794758
类文件
这节课的地址不能用了,所以post请求返回的的不是200而是403
package cn.itcast.crawler.test;
import com.sun.org.glassfish.gmbal.NameValue;
import org.apache.hc.client5.http.classic.methods.HttpPost;
imp...