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

    }
}
相关推荐
Luck_ff08108 小时前
【Python爬虫详解】第四篇:使用解析库提取网页数据——BeautifuSoup
开发语言·爬虫·python
天天扭码16 小时前
什么?2025年了还在写传统爬虫!来试试更有爽感的AI爬虫 ψ(`∇´)ψ
爬虫·node.js·ai编程
梯度寻优者_超16 小时前
获取房源信息并完成可视化——网络爬虫实战1
爬虫
MinggeQingchun1 天前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
kadog1 天前
PubMed PDF下载 cloudpmc-viewer-pow逆向
前端·javascript·人工智能·爬虫·pdf
nongcunqq1 天前
爬虫练习 js 逆向
笔记·爬虫
一个天蝎座 白勺 程序猿1 天前
Python爬虫(5)静态页面抓取实战:requests库请求头配置与反反爬策略详解
开发语言·爬虫·python
iuhart1 天前
chromedp 反反爬设计方案
爬虫
橘猫云计算机设计1 天前
net+MySQL中小民营企业安全生产管理系统(源码+lw+部署文档+讲解),源码可白嫖!
数据库·后端·爬虫·python·mysql·django·毕业设计
爱吃泡芙的小白白1 天前
爬虫学习——使用HTTP服务代理、redis使用、通过Scrapy实现分布式爬取
redis·分布式·爬虫·http代理·学习记录