get请求搜索功能爬虫

<!--爬虫仅支持1.8版本的jdk-->

<!-- 爬虫需要的依赖-->

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.5.2</version>

</dependency>

<!-- 爬虫需要的日志依赖-->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.25</version>

</dependency>

爬虫配置文件位置及存放位置

复制代码
package day02;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;

public class pacohngde {
    public static void main(String[] args) throws IOException, URISyntaxException {
        //注意这个方法是爬取网址所有位置
        
        //1.打开浏览器,创建Httpclient对象
       //        CloseableHttpclient httpclient = Httpclients.createDefault();
        CloseableHttpClient aDefault = HttpClients.createDefault();
        
     // 组合示例https://search.bilibili.com/all?keyword=药水哥&search_source=1
        //创建URTBuilder  说白就是把网站组合起来使用搜索功能
        URIBuilder uriBuilder = new URIBuilder("https://search.bilibili.com/all");
       //设置参数
        uriBuilder.setParameter("keyword","药水哥").setParameter("search_source","1");
     //2.输入网址,发起get请求创建HttpGet对象 输入你需要爬取的网址
     HttpGet httpGet = new HttpGet(uriBuilder.build());

     System.out.println("要爬取的网址"+httpGet);

        //3.按回车,发起请求,返回响应,使用httpclient对象发起请求
        CloseableHttpResponse response = aDefault.execute(httpGet);
        
        //4.解析响应,获取数据//判断状态码是否是200     200为正常型号  其他为异常
        if(response.getStatusLine().getStatusCode()== 200){
            //获取爬取数据
            HttpEntity httpEntity =response.getEntity();
            //将爬取数据解析为utf-8格式
          String content = EntityUtils.toString(httpEntity,"utf8");
          //打印
                System.out.println(content);

}
        //释放资源
        response.close();
        //关闭网页
        aDefault.close();

    }
}
相关推荐
cipher5 天前
crawl4ai:AI时代的数据采集利器——从入门到实战
后端·爬虫·python
深蓝电商API5 天前
结构化数据提取:XPath vs CSS 选择器对比
爬虫·python
易辰君6 天前
【Python爬虫实战】正则:中文匹配与贪婪非贪婪模式详解
开发语言·爬虫·python
深蓝电商API6 天前
爬虫增量更新:基于时间戳与哈希去重
爬虫·python
电商API_180079052476 天前
京东商品评论API接口封装的心路历程
服务器·开发语言·爬虫·数据分析·php
袁袁袁袁满6 天前
Haystack与亮数据MCP工具结合实现自动化爬虫
爬虫·python·网络爬虫·数据采集·爬虫实战·视频爬虫·特推爬虫
深蓝电商API6 天前
Redis 作为爬虫去重与任务队列实战
爬虫·python
IP搭子来一个6 天前
爬虫使用代理IP全解析:原理、类型与实战指南
爬虫·网络协议·tcp/ip
iFeng的小屋6 天前
【2026最新xhs爬虫】用Python批量爬取关键词笔记,异步下载高清图片!
笔记·爬虫·python
嫂子的姐夫7 天前
030-扣代码:湖北图书馆登录
爬虫·python·逆向