网络爬虫-爬取微博热门话题前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('\n');
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,内容如下:

转载于:https://www.cnblogs.com/miaomiaokaixin/p/5974288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值