暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【经验与坑】巧妙利用chatgpt快速完成爬虫需求

当前有这样的需求:

(1)爬取bilibili弹幕视频网站某条视频下的500条评论

(2)存储到xls格式文件中


解决方案:

(1)根据bilbili网站评论区API request到json格式评论

(2)基于阿里FASTJSON解析json数据,获取到想要的评论字符串

(3)将评论字符串塞进实体类结构中

(4)基于阿里POI文件处理机制生成EXCEL文件

(5)IO流导出文件,完成任务


一.根据chatgpt生成爬虫代码

       一开始并没有想到用gpt,但是在google上搜索出来的结果都比较恶劣,充满了广告和无法实现的方案。

尝试了gpt 如下图所示

根据gpt生成的实例代码进行改进。如下所示:

    String cid = "BVxxxxxx"; // 评论所属的视频ID
    int pageNo = 30; // 评论的页数
    int pageSize = 20; // 每页的评论数


    List<CommentEntity> commentEntities = new ArrayList<>();
    //爬取数据
    for (int j=1;j<=pageNo;j++){
    try {
    String url = "https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=" + j + "&type=1&oid=" + cid + "&sort=0&nohot=1&ps=" + pageSize;
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");


    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuilder response = new StringBuilder();


    while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
    }
    in.close();


    String commentJson = response.toString(); // 获取到的评论JSON数据


    //解析json数据
    JSONObject rootjson = JSONObject.parseObject(commentJson);
    JSONObject datajson = (JSONObject) rootjson.get("data");
    JSONArray repliesArray = datajson.getJSONArray("replies");
    for (Object o : repliesArray) {
    JSONObject reply = (JSONObject) o;
    JSONObject content = reply.getJSONObject("content");
    String msg = content.getString("message");
    CommentEntity commentEntity = new CommentEntity(msg);
    commentEntities.add(commentEntity);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    复制

    (Comment实体类中只有comment字符串一个字段)


    二.解析JSON字符串

    使用FASTJSON依赖进行解析整理。(插入依赖)

              <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.62</version>
      </dependency>
      复制

      观察数据,发掘一下我们需要的评论字段在哪层

      (这里使用了vscode+插件json tools 快捷键ctrl+alt+m) 一键格式化json文件。

      经过json文件分析,我们需要拿到 data JSONObject 然后再拿到replies JSONArray 然后再循环得到cotent JSONObject,最后得到content下面的message字符串。

      三.导入excel文件

      导入poi依赖

                <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.10.1</version>
        </dependency>
        复制

        此部分代码如下所示

          //写入excel文件
          HSSFWorkbook workbook = new HSSFWorkbook();
          HSSFSheet sheet= workbook.createSheet();
          HSSFRow header= sheet.createRow(0);
          HSSFCellStyle cellStyle= workbook.createCellStyle();
          cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
          HSSFCell cell= header.createCell(0);
          cell.setCellStyle(cellStyle);
          cell.setCellValue("评论");


          for(int i=0;i< commentEntities.size();i++){
          HSSFRow content = sheet.createRow(i+1);
          content.createCell(0).setCellValue(commentEntities.get(i).getComment());
          }
          //写入文件
          try {
          FileOutputStream fileOutputStream = new FileOutputStream("/data/workarea/PythonArea/weizhendataset/bili_comments.xls");
          workbook.write(fileOutputStream);
          fileOutputStream.close();
          System.out.println("导出完成!");
          }catch (IOException e){
          e.printStackTrace();
          System.out.println("导出失败!");
          }
          复制


          四.结果显示

          剃点词效果展示:

          五.总结与展望

                 借用师弟的话,作者表示十分赞同。写码的新时代即将到来,会不会善用AI将成为水平的分水岭!chatgpt的出现将加大大家的差距!

                 在这样效率为王的时代,业务实现领域,站在企业的角度看当前需要程序工作者快速完成所交代的需求任务,chatgpt的出现极大地加快了程序工作者的工作效率。科幻的未来就在眼前!

          文章转载自码农智涵的程序人生,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论