网络爬虫-爬取微博热门话题前15个

网络爬虫-爬取微博热门话题前15个

2023年6月29日发(作者:)

⽹络爬⾍-爬取微博热门话题前15个⽤java+webdriver+testng实现获取微博热门话题前15个,包括话题排名、标题、阅读量、内容,写⼊txt⽂件功能前提条件:已安装好java环境,⼯程导⼊了webdriver的jar包和testng的jar包代码如下:第⼀:新建PublicModel类,该类中实现了写⼊txt的⽂件功能和初始化⽅法 1 package s; 2

3 import ; 4 import tputStream; 5 import ption; 6 import ist; 7 import p; 8

9 import ver;10 import etExplorerDriver;11

12 public class PublicModel {13 public static WebDriver driver;14

15 /**16 * 初始化⽅法17 */18 public static void initModel() {19 driver = new InternetExplorerDriver();20 /*().timeouts().implicitlyWait(3, S);*/21 ().window().maximize();22 }23

24

25

26 /**27 * 写⼊txt⽂件⽅法数组28 *

29 * @param hotTopics30 * hashmap的数组内容31 * @param file32 * ⽂件名称33 * @throws IOException34 */35 public static void writeContent(ArrayList> hotTopics, String file) throws IOException {36 /* ⽂件名:当前⼯程路径+result+ */37 String filename = perty("") + tor + "result" + tor + file + ".txt";38 FileOutputStream fis = new FileOutputStream(filename);39

40 /* 遍历arrayList的hashMap内容,按⾏写⼊txt⽂件 */41 for (int i = 0; i < (); i++) {42 byte[] a = (i).toString().getBytes();43 (a);44 ('n');45 }46 ();47 }48

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

3 import ist; 4 import p; 5 import ; 6

7 import ; 8 import ment; 9 import lass;10 import Class;11 import ;12

13 import Model;14

15

16 public class BlogTopic extends PublicModel {17

18 @BeforeClass19 public void setUp() {20 initModel();21 }22

23 /**24 * 获取微博热门话题前15个,包括话题排名、标题、阅读量、内容,写⼊txt⽂件25 * @throws Exception26 */27 @Test28 public void getHotTopic() throws Exception {29 String url = "/100803?cfs=&Pl_Discover_Pt6Rank__5_filter=hothtlist_type%3D1#_0";30 (url);31 /* 获取微博热门话题根节点 */32 WebElement rootNode = ement(("Pl_Discover_Pt6Rank__5"))33 .findElement(ector("ul[class^='pt_ul']"));34 List nodes = ements(ector("li[class^='pt_li']"));35 /* 遍历添加话题排名、标题、阅读数、内容到数组中 */36 ArrayList> hotTopics = new ArrayList>();37 for (WebElement node : nodes) {38 HashMap topic = new HashMap();39 ("正⽂链接", ement(ame("S_txt1")).getAttribute("href").toString());40 ("阅读量", ement(ame("number")).getText());41 ("话题排名", ement(ector("span[class^='DSC_topicon']")).getText());42 ("标题", ement(ame("S_txt1")).getText());43 (topic);44 }45 /*数组数据写⼊txt*/46 writeContent(hotTopics,"blogtopic");47 }48

49 @AfterClass50 public void quit() {51 ();52 }53

54 }第三:配置⽂件1 2 3 4 5 6 7 8 9 运⾏结果为:项⽬路径result⽬录下⽣成了⼀个⽂件:,内容如下:

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687983481a63643.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信