zoukankan      html  css  js  c++  java
  • 网络爬虫-爬取微博热门话题前15个

    用java+webdriver+testng实现获取微博热门话题前15个,包括话题排名、标题、阅读量、内容,写入txt文件功能

    前提条件:

    已安装好java环境,工程导入了webdriver的jar包和testng的jar包

    代码如下:

    第一:新建PublicModel类,该类中实现了写入txt的文件功能和初始化方法

     1 package com.ustc.publics;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.util.ArrayList;
     7 import java.util.HashMap;
     8 
     9 import org.openqa.selenium.WebDriver;
    10 import org.openqa.selenium.ie.InternetExplorerDriver;
    11 
    12 public class PublicModel {
    13     public static WebDriver driver;
    14 
    15     /**
    16      * 初始化方法
    17      */
    18     public static void initModel() {
    19         driver = new InternetExplorerDriver();
    20          /*driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);*/
    21         driver.manage().window().maximize();
    22     }
    23 
    24     
    25 
    26     /**
    27      * 写入txt文件方法数组
    28      * 
    29      * @param hotTopics
    30      *            hashmap的数组内容
    31      * @param file
    32      *            文件名称
    33      * @throws IOException
    34      */
    35     public static void writeContent(ArrayList<HashMap<String, String>> hotTopics, String file) throws IOException {
    36         /* 文件名:当前工程路径+result+file.txt */
    37         String filename = System.getProperty("user.dir") + File.separator + "result" + File.separator + file + ".txt";
    38         FileOutputStream fis = new FileOutputStream(filename);
    39 
    40         /* 遍历arrayList的hashMap内容,按行写入txt文件 */
    41         for (int i = 0; i < hotTopics.size(); i++) {
    42             byte[] a = hotTopics.get(i).toString().getBytes();
    43             fis.write(a);
    44             fis.write('
    ');
    45         }
    46         fis.close();
    47     }
    48 
    49 }


    第二:新建BlogTopic类,该类继承了PublicModel类,实现功能为获取微博热门话题15个,包括话题排名、标题、阅读量、内容

     1 package com.ustc.base;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 
     7 import org.openqa.selenium.By;
     8 import org.openqa.selenium.WebElement;
     9 import org.testng.annotations.AfterClass;
    10 import org.testng.annotations.BeforeClass;
    11 import org.testng.annotations.Test;
    12 
    13 import com.ustc.publics.PublicModel;
    14 
    15 
    16 public class BlogTopic extends PublicModel {
    17 
    18     @BeforeClass
    19     public void setUp() {
    20         initModel();
    21     }
    22 
    23     /**
    24      * 获取微博热门话题前15个,包括话题排名、标题、阅读量、内容,写入txt文件
    25      * @throws Exception
    26      */
    27     @Test
    28     public void getHotTopic() throws Exception {
    29         String url = "http://d.weibo.com/100803?cfs=&Pl_Discover_Pt6Rank__5_filter=hothtlist_type%3D1#_0";
    30         driver.get(url);
    31         /* 获取微博热门话题根节点 */
    32         WebElement rootNode = driver.findElement(By.id("Pl_Discover_Pt6Rank__5"))
    33                 .findElement(By.cssSelector("ul[class^='pt_ul']"));
    34         List<WebElement> nodes = rootNode.findElements(By.cssSelector("li[class^='pt_li']"));
    35         /* 遍历添加话题排名、标题、阅读数、内容到数组中 */
    36         ArrayList<HashMap<String, String>> hotTopics = new ArrayList<HashMap<String, String>>();
    37         for (WebElement node : nodes) {
    38             HashMap<String, String> topic = new HashMap<String, String>();
    39             topic.put("正文链接", node.findElement(By.className("S_txt1")).getAttribute("href").toString());
    40             topic.put("阅读量", node.findElement(By.className("number")).getText());
    41             topic.put("话题排名", node.findElement(By.cssSelector("span[class^='DSC_topicon']")).getText());
    42             topic.put("标题", node.findElement(By.className("S_txt1")).getText());
    43             hotTopics.add(topic);
    44         }
    45         /*数组数据写入txt*/
    46         writeContent(hotTopics,"blogtopic");
    47     }
    48 
    49     @AfterClass
    50     public void quit() {
    51         driver.quit();
    52     }
    53 
    54 }

    第三:配置testng.xml文件

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    3 <suite name="Suite" parallel="false">
    4   <test name="Test">
    5     <classes>
    6       <class name="com.ustc.base.BlogTopic"/>    <!--1:微博热门话题  -->
    7     </classes>
    8   </test> <!-- Test -->
    9 </suite> <!-- Suite -->

    运行testng.xml结果为:
    项目路径result目录下生成了一个文件:blogtopic.txt,内容如下:

  • 相关阅读:
    学习笔记CB002:词干提取、词性标注、中文切词、文档分类
    学习笔记CB001:NLTK库、语料库、词概率、双连词、词典
    从零开始在iPhone上运行视频流实时预测模型应用,只需10步
    学习笔记DL008:概率论,随机变量,概率分布,边缘概率,条件概率,期望、方差、协方差
    学习笔记DL007:Moore-Penrose伪逆,迹运算,行列式,主成分分析PCA
    学习笔记DL005:线性相关、生成子空间,范数,特殊类型矩阵、向量
    学习笔记DL004:标量、向量、矩阵、张量,矩阵、向量相乘,单位矩阵、逆矩阵
    学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击
    学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
    学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
  • 原文地址:https://www.cnblogs.com/miaomiaokaixin/p/5974288.html
Copyright © 2011-2022 走看看