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

    }
}
相关推荐
ZC跨境爬虫11 小时前
Scrapy多级请求实战:5sing伴奏网爬取踩坑与优化全记录(JSON提取+Xpath解析)
爬虫·scrapy·html·json
willhuo12 小时前
基于Playwright的抖音网页自动化浏览器项目使用指南
爬虫·c#·.netcore·webview
-To be number.wan15 小时前
Python爬取百度指数保姆级教程
爬虫·python
程序员老邢16 小时前
【产品底稿 04】商助慧 V1.1 里程碑:爬虫入库 + MySQL + Milvus 全链路打通
java·爬虫·mysql·ai·springboot·milvus
ZC跨境爬虫1 天前
【爬虫实战对比】Requests vs Scrapy 笔趣阁小说爬虫,从单线程到高效并发的全方位升级
前端·爬虫·scrapy·html
ZC跨境爬虫1 天前
【Scrapy实战避坑】5sing网站爬虫从0到1,踩遍动态渲染、正则匹配全坑(附完整解决方案)
爬虫·scrapy
ZC跨境爬虫2 天前
Scrapy实战爬取5sing网站:Pipeline优化+全流程踩坑复盘,从报错到数据落地
前端·爬虫·python·scrapy
码农很忙2 天前
爬虫与反爬虫攻防战:技术解析与实战指南
爬虫
大數據精準工單獲取2 天前
【数据抓取】 编写爬虫基本请求:使用爬虫框架发送 HTTP 请求,获取网页内容
爬虫·网络协议·http
IP老炮不瞎唠2 天前
为什么Python爬虫需要代理 IP?原理与应用详解
爬虫·python·tcp/ip