利用WebCollector+selenium抓取新浪微博数据

本文介绍了如何结合WebCollector和Selenium库抓取新浪微博的数据。提供了必要的依赖包下载链接,并给出了两个关键类WeiboCN.java和WeiboCrawler.java的代码示例。作者罗坚元分享了详细的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

抓下来返回text是这样的:

微博数据

需要用到的包:下载地址:http://pan.baidu.com/s/1o69myOq

爬虫所用

两个类的代码
WeiboCN.java

import java.util.Set;
import cn.edu.hfut.dmic.webcollector.*;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class WeiboCN {
    /**
     * 代码由WebCollector提供,如果不在WebCollector中使用,需要导入selenium相关jar包
     */

        /**
         * 获取新浪微博的cookie,这个方法针对weibo.cn有效,对weibo.com无效
         * weibo.cn以明文形式传输数据,请使用小号
         * @param username 新浪微博用户名
         * @param password 新浪微博密码
         * @return
         * @throws Exception 
         */
        public static String getSinaCookie(String username, String password) throws Exception{
            StringBuilder sb = new StringBuilder();
            HtmlUnitDriver driver = new HtmlUnitDriver();
            driver.setJavascriptEnabled(true);
            driver.get("http://login.weibo.cn/login/");

            WebElement mobile = driver.findElementByCssSelector("input[name=mobile]");
            mobile.sendKeys(username);
            WebElement pass = driver.findElementByCssSelector("input[name^=password]");
            pass.sendKeys(password);
            WebElement rem = driver.findElementByCssSelector("input[name=remember]");
            rem.click();
            WebElement submit = driver.findElementByCssSelector("input[name=submit]");
            submit.click();

            Set<Cookie> cookieSet = driver.manage().getCookies();
            driver.close();
            for (Cookie cookie : cookieSet) {
                sb.append(cookie.getName()+"="+cookie.getValue()+";");
            }
            String result=sb.toString();
            if(result.contains("gsid_CTandWM")){
                return result;
            }else{
                throw new Exception("weibo login failed");
            }
        }
}

WeiboCrlawer.java

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import cn.edu.hfut.dmic.webcollector.crawler.DeepCrawler;  
import cn.edu.hfut.dmic.webcollector.model.Links;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.net.HttpRequesterImpl;
public class WeiboCrawler extends DeepCrawler{
    public WeiboCrawler(String crawlPath) throws Exception {  
        super(crawlPath);  
        /*获取新浪微博的cookie,账号密码以明文形式传输,请使用小号*/  
        String cookie=WeiboCN.getSinaCookie("你的微博账号", "密码");  
        HttpRequesterImpl myRequester=(HttpRequesterImpl) this.getHttpRequester();  
        myRequester.setCookie(cookie);  
    }  

    public Links visitAndGetNextLinks(Page page) {  
        /*抽取微博*/  
        Elements weibos=page.getDoc().select("div.c");  
        for(Element weibo:weibos){  
            System.out.println(weibo.text());  
        }  
        /*如果要爬取评论,这里可以抽取评论页面的URL,返回*/  
        return null;  
    }  

    public static void main(String[] args) throws Exception{  
        WeiboCrawler crawler=new WeiboCrawler("/home/hu/data/weibo");  
        crawler.setThreads(3);  
        /*对某人微博前5页进行爬取*/  
        for(int i=0;i<5;i++){  
            crawler.addSeed("http://weibo.cn/vipgcu?vt=4&page="+i);  
        }  
        crawler.start(1);  
    }
}

本文作者:By: 罗坚元 :http://blog.youkuaiyun.com/sunyuan_software

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值