数据分析之Java抓取搜狗指数数据

在处理数据分析的时候,如果我们的数据来源于网页,则需要通过数据爬虫来实现将网页端的数据转化到本地存储,再进行分析。
在这篇文章中,我们将介绍一步步介绍从搜狗指数下载数据到Excel文件中。

分析搜狗指数网页

想要从一个网页下载数据,首先需要了解网页中的数据结构,这里以宠物搜索数据为例。

对于这样的页面数据

搜狗指数-宠物

通过F12进入到网页的源代码,可以看到

搜狗指数-宠物-源码

接下来,我们就是想办法将在script标签中的数据导出来

通过Java将搜狗指数数据保存到Excel中

  • 通过Jsoup获取页面源码
    1
    2
    Document doc = Jsoup.connect(url).get();
    String script = doc.selectFirst("script").html();
  • 处理script标签提取json数据到对象集合
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Pattern p = Pattern.compile("root.SG.wholedata = (.*)", Pattern.MULTILINE);
    Matcher matcher = p.matcher(script);
    String wholedata = "";
    while(matcher.find()) {
    wholedata = matcher.group(1);
    }
    ObjectMapper objectMapper = new ObjectMapper();
    SougouData sougouData = objectMapper.readValue(wholedata, SougouData.class);
    for (Pv pv : sougouData.pvList.get(0)) {
    IndexTrend indexTrend = new IndexTrend();
    indexTrend.date = pv.date;
    indexTrend.PV = pv.pv;
    indexTrends.add(indexTrend);
    }
  • 保存对象到Excel文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public void SaveToExcel(Set<IndexTrend> indexTrends) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Pet");
    int rowNum = 0;
    for (IndexTrend indexTrend : indexTrends) {
    Row row = sheet.createRow(rowNum++);
    Cell date = row.createCell(0);
    Cell pv = row.createCell(1);
    CellStyle cellStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cellStyle.setDataFormat(
    createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
    date.setCellValue(indexTrend.date);
    date.setCellStyle(cellStyle);
    pv.setCellValue(indexTrend.PV);
    }
    FileOutputStream outputStream = new FileOutputStream("./Download Sougou To Excel/petdata.xlsx");
    workbook.write(outputStream);
    workbook.close();
    }

数据网址

兵马未动,粮草先行。想要做数据分析,首先需要有相关的数据。

一般来说,数据来源于企业或者个人的自有数据,而对于企业或者个人数据量有限,有业务需求的时候通常会通过数据调研公司来购买数据。
而网络上也有些共用且免费的数据,来帮助我们辅助分析。

这里用于搜集分享所了解和使用的网络数据(不定期更新):

  • 搜狗指数
    • 简介
      • 以搜狗搜索海量网民行为数据和微信公众平台数据为基础的综合数据分析统计平台,在这里您可以查看全网热门事件、品牌、人物等等一系列查询词的搜索热度和微信热度变化趋势,从而掌握网民需求变化和媒体舆论关注热点变化;也可以从行业的角度,分析市场特点。
    • 特点
      • 网页自带图表,可以分析趋势
      • 原始数据在网页html script中,可以通过爬虫获取到原始数据,并进行分析

Java极简入门之表达式

If 表达式

  • 语法
    1
    2
    3
    if (condition) {
    // block of code to be executed if the condition is true
    }
  • 示例
    1
    2
    3
    if (20 > 18) {
    System.out.println("20 is greater than 18");
    }

    Switch

  • 语法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    switch(expression) {
    case x:
    // code block
    break;
    case y:
    // code block
    break;
    default:
    // code block
    }
  • 示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    int day = 4;
    switch (day) {
    case 1:
    System.out.println("Monday");
    break;
    case 2:
    System.out.println("Tuesday");
    break;
    case 3:
    System.out.println("Wednesday");
    break;
    case 4:
    System.out.println("Thursday");
    break;
    case 5:
    System.out.println("Friday");
    break;
    case 6:
    System.out.println("Saturday");
    break;
    case 7:
    System.out.println("Sunday");
    break;
    }
    // Outputs "Thursday" (day 4)

While Loop

  • 语法
    1
    2
    3
    while (condition) {
    // code block to be executed
    }
  • 示例
    1
    2
    3
    4
    5
    int i = 0;
    while (i < 5) {
    System.out.println(i);
    i++;
    }

    Do/While Loop

  • 语法
    1
    2
    3
    4
    do {
    // code block to be executed
    }
    while (condition);
  • 示例
    1
    2
    3
    4
    5
    6
    int i = 0;
    do {
    System.out.println(i);
    i++;
    }
    while (i < 5);

    For Loop

  • 语法
    1
    2
    3
    for (statement 1; statement 2; statement 3) {
    // code block to be executed
    }
  • 示例
    1
    2
    3
    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }

    For-Each Loop

  • 语法
    1
    2
    3
    for (type variableName : arrayName) {
    // code block to be executed
    }
  • 示例
    1
    2
    3
    4
    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    for (String i : cars) {
    System.out.println(i);
    }

    Break

  • break用来“跳出”一条switch语句。
  • break语句还可以用于跳出 循环
    1
    2
    3
    4
    5
    6
    for (int i = 0; i < 10; i++) {
    if (i == 4) {
    break;
    }
    System.out.println(i);
    }

Continue

continue如果出现指定条件,该语句将中断一次迭代(在循环中),并在循环中继续进行下一次迭代

1
2
3
4
5
6
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}

Java极简入门之变量

Java变量

变量用于存储值和对象

定义变量

1
type variable = value;
  • type是数据类型,可以是基础数据类型,也可以是引用数据类型
  • variable是变量的名字
  • value 是变量的值

示例

1
2
3
4
5
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

Java极简入门之数据类型

基础数据类型

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

引用数据类型

  • Strings
  • Arrays
  • Classes
  • Interface

基础数据类型与引用数据类型的区别

  • 基本类型在Java中是预定义的(已经定义)。非基本类型是由程序员创建的,而不是由Java定义的(除外String)。
  • 非原始类型可用于调用方法以执行某些操作,而原始类型则不能。
  • 基本类型始终具有一个值,而非基本类型可以是null。
  • 基本类型以小写字母开头,而非基本类型以大写字母开头。
  • 基本类型的大小取决于数据类型,而非基本类型具有相同的大小。

Java极简入门之Hello World

创建Java项目

  • VS Code中按下快捷键Ctrl+Shift+P
  • 输入Java: Create Java Project
  • 输入项目名称
  • F5运行项目

示例代码

1
2
3
4
5
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}
  • App
    • class(类名)
  • main
    • 程序的执行入口
    • 每一个程序都必须有
  • System.out.println()
    • 输出文本

F5执行代码,并在输出窗口查看结果

Java极简入门之安装

Java安装和IDE使用

一般来说,Java IDE会选择Eclipse,为了简化环境配置中的步骤,这里提供VS Code开发Java的方式

VS Code

  • 为VS Code 安装 Java Extension Pack(包含了以下的组件)
    • Language Support for Java(TM) by Red Hat
    • Debugger for Java
    • Java Test Runner
    • Maven for Java
    • Project Manager for Java
  • 配置JDK
    • VS Code中按下快捷键Ctrl+Shift+P
    • 输入Java: Configure Java Runtime
    • 如果已经下载了想要的JDK,可以直接选择使用
    • 如果未下载,可以直接选择安装,安装完重新加载VS Code
  • 创建Java项目
    • VS Code中按下快捷键Ctrl+Shift+P
    • 输入Java: Create Java Project
    • 输入项目名称
    • F5运行项目

引用

FAQ

  • Q: Cannot resolve the modulepaths/classpaths automatically, please specify the value in the launch.json
    A: 按下快捷键Ctrl+Shift+P,输入Clean Java Language Server Workspace,重新运行项目

VBA之方法函数

方法函数

  • Sub
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ' Declares a procedure named GetInfo 
    ' This Sub procedure takes no arguments
    Sub GetInfo()
    ' Declares a string variable named answer
    Dim answer As String
    ' Assigns the return value of the InputBox function to answer
    answer = InputBox(Prompt:="What is your name?")
    ' Conditional If...Then...Else statement
    If answer = Empty Then
    ' Calls the MsgBox function
    MsgBox Prompt:="You did not enter a name."
    Else
    ' MsgBox function concatenated with the variable answer
    MsgBox Prompt:="Your name is " & answer
    ' Ends the If...Then...Else statement
    End If
    ' Ends the Sub procedure
    End Sub
  • Function
    1
    2
    3
    Function Celsius(fDegrees) 
    Celsius = (fDegrees - 32) * 5 / 9
    End Function

Sub或Function 调用

1
2
3
4
5
6
7
8
9
10
11
12
Sub Main() 
HouseCalc 99800, 43100
Call HouseCalc(380950, 49500)
End Sub

Sub HouseCalc(price As Single, wage As Single)
If 2.5 * wage <= 0.8 * price Then
MsgBox "You cannot afford this house."
Else
MsgBox "This house is affordable."
End If
End Sub