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();

    }
}
相关推荐
deepwater_zone13 分钟前
网络爬虫(web crawler)
爬虫
华科云商xiao徐8 小时前
告别IP被封!分布式爬虫的“隐身”与“分身”术
爬虫·数据挖掘·数据分析
q5673152312 小时前
告别低效:构建健壮R爬虫的工程思维
开发语言·爬虫·r语言
一个天蝎座 白勺 程序猿21 小时前
Python爬虫(47)Python异步爬虫与K8S弹性伸缩:构建百万级并发数据采集引擎
爬虫·python·kubernetes
华科云商xiao徐1 天前
告别低效:构建健壮R爬虫的工程思维
爬虫
熊猫钓鱼>_>2 天前
2025反爬虫之战札记:从robots.txt到多层防御的攻防进化史
开发语言·c++·爬虫
Blurpath2 天前
如何利用静态代理IP优化爬虫策略?从基础到实战的完整指南
爬虫·网络协议·ip代理·住宅代理
wjayg2252 天前
网络爬虫是自动从互联网上采集数据的程序
爬虫
IT毕设实战小研2 天前
2026届大数据毕业设计选题推荐-基于大数据旅游数据分析与推荐系统 爬虫数据可视化分析
大数据·人工智能·爬虫·机器学习·架构·数据分析·课程设计
Villiam_AY3 天前
使用 chromedp 高效爬取 Bing 搜索结果
后端·爬虫·golang