jsoup是一款Java的HTML解析器,主要用来对HTML解析
导入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
Java代码
@Test
void contextLoads2() throws IOException {
String url = "https://sdrhser.baidu.com/s?id=1665515378178745245&wfr=spider&for=pc";
Connection conn = Jsoup.connect(url).timeout(3000);
//设置请求头,模拟浏览器登陆,绕过简单的反爬虫机制
conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.header("Accept-Encoding", "gzip, deflate, sdch");
conn.header("Accept-Language", "zh-CN,zh;q=0.8");
conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
//从url中获取流并解析为Document对象
Document document = conn.get();
Elements elements = document.getElementsByClass("img-container");
for (Element element : elements) {
Elements img = element.getElementsByTag("img");
for (int i = 0; i < img.size(); i++) {
String src = img.eq(i).attr("src");
System.out.println(src);
}
}
}
爬取结果如下
2020-10-18 21:25:49.011 INFO 10732 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2020-10-18 21:25:49.032 INFO 10732 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2020-10-18 21:25:49.058 INFO 10732 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2020-10-18 21:25:49.244 INFO 10732 --- [ main] c.l.s.Springboot01ApplicationTests : Started Springboot01ApplicationTests in 11.456 seconds (JVM running for 14.219)
https://pics0.baidu.com/feed/32fa828ba61ea8d3556bfacb2147a948241f580c.jpeg?token=bbc10ca3cd821e69885f1a81aef2daec
https://pics5.baidu.com/feed/77c6a7efce1b9d16853f6a2c4b932d898d546453.jpeg?token=a295597413f075effa421cc8fa8d0c9f
https://pics1.baidu.com/feed/1f178a82b9014a90716d5eb21e3aa014b21beec6.jpeg?token=930c3d827d17beeea3af30a7020889b3
https://pics4.baidu.com/feed/2cf5e0fe9925bc310e80c38de69214b7ca13709e.jpeg?token=9e0b4a5da4ef4e38e0404a64e31329ee&s=015031D1989F7BDAD808D4030100E0E0
https://pics0.baidu.com/feed/dcc451da81cb39dbd26674f1675b9022aa183042.jpeg?token=5f98e36fc80f71a9a52f425ae627e267
https://pics7.baidu.com/feed/1ad5ad6eddc451da66a199ed0eb0cb60d0163231.jpeg?token=94fd543c08af98f7c4e7e97f0282197f
https://pics2.baidu.com/feed/2934349b033b5bb59f4ea562819e4c3fb700bc61.jpeg?token=8ace1a88c84b14f5f0977ac7223ed139
https://pics6.baidu.com/feed/0dd7912397dda144c2d1f7c604fa49a40df486ba.jpeg?token=b4bf13fcedd4cf61a1506e6df42fa55b
https://pics0.baidu.com/feed/203fb80e7bec54e70f03d6ef0c7502564ec26a2c.jpeg?token=43ffb710ca0798241285ff4363d49dfc
https://pics0.baidu.com/feed/730e0cf3d7ca7bcbc96edc120644f265f624a84e.jpeg?token=539ed150f7f5a9b567af603967725a4b
https://pics6.baidu.com/feed/dc54564e9258d109a701dd8a661555b96d814dff.jpeg?token=2f645183b556ee55a04404cef1b494f1
https://pics0.baidu.com/feed/908fa0ec08fa513d68d7cd0a8b20ccfdb3fbd9bd.jpeg?token=e337fa4827d9918ebc8440b59136766f
https://pics2.baidu.com/feed/279759ee3d6d55fb68a348dbda6fd64c21a4ddbe.jpeg?token=88e22981f76e465b49aefb4f5cbc821f
https://pics4.baidu.com/feed/6c224f4a20a446237366e1442f6feb080cf3d738.jpeg?token=ae4f0c3a5f2833a51c3555bd6939fa25
https://pics3.baidu.com/feed/f7246b600c3387441a261261e94240ffd62aa0a4.jpeg?token=1014225dcef5966735961c089113f529
2020-10-18 21:25:50.416 INFO 10732 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler'
2020-10-18 21:25:50.418 INFO 10732 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-10-18 21:25:50.475 INFO 10732 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-0} closing ...
Process finished with exit code 0