读书人

未导入jar资料

发布时间: 2013-09-14 13:03:22 作者: rapoo

未导入jar文件
下面这个程序是网上的一段关于登陆人人网然后抓取相关信息的程序,但是未导入jar文件包,小弟刚刚学习java不久,望大神们能给写全import jar文件,我导入了一些,但是还是报错,希望各位大神帮忙看一下,看看是程序的问题还是jar文件不全的问题,这个程序对我很有用,大神们一定要帮帮小弟
public
class RenRen {
// The configuration items

private
static String userName = "YourMailinRenren";
private
static String password = "YourPassword";
private
static String redirectURL = "http://blog.renren.com/blog/304317577/449470467";


// Don't change the following URL

private
static String renRenLoginURL = "http://www.renren.com/PLogin.do";


// The HttpClient is used in one session

private HttpResponse response;
private DefaultHttpClient httpclient = new DefaultHttpClient();


private
boolean login() {
HttpPost httpost = new HttpPost(renRenLoginURL);
// All the parameters post to the web site

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("origURL", redirectURL));
nvps.add(new BasicNameValuePair("domain", "renren.com"));
nvps.add(new BasicNameValuePair("isplogin", "true"));
nvps.add(new BasicNameValuePair("formName", ""));
nvps.add(new BasicNameValuePair("method", ""));
nvps.add(new BasicNameValuePair("submit", "登录"));
nvps.add(new BasicNameValuePair("email", userName));
nvps.add(new BasicNameValuePair("password", password));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


response = httpclient.execute(httpost);
} catch (Exception e) {
e.printStackTrace();
return
false;
} finally {
httpost.abort();
}
return
true;
}


private String getRedirectLocation() {
Header locationHeader = response.getFirstHeader("Location");
if (locationHeader == null) {
return
null;
}
return locationHeader.getValue();
}


private String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
httpclient.getConnectionManager().shutdown();


}
return responseBody;
}


public
void printText() {
if (login()) {
String redirectLocation = getRedirectLocation();
if (redirectLocation != null) {
System.out.println(getText(redirectLocation));
}
}
}


public
static
void main(String[] args) {
RenRen renRen = new RenRen();
renRen.printText();
}
}
jar java
[解决办法]
你前面一定有:
import org.apache.commons.httpclient.Header或
import org.apache.commons.httpclient.*

改成:
import org.apache.http.Header
就行了,或者:
org.apache.http.Header locationHeader = response.getFirstHeader("Location");
[解决办法]

引用:
Quote: 引用:

这不是导入jar文件,只是普通的导入引入包而已吧。
按ctrl+shift+o
如果提示导入包选择的话选择java.net包里面的。
如果全部导入完了还不全的话,那就是缺失jar包

大神我想做的是访问一个带有登陆端的网页,然后进去他的子网页抓取一些数据,现在实现了登陆,也实现了自动抓取,就用人人网举例吧,登陆程序就是上面的程序我想进入我的一片日志去抓取里面的文本,抓取程序是
void writefile() throws IOException
{ //FileWriter fileWriter=new FileWriter("C:\\shuju.txt");
//String getUrl = "http://211.82.113.58/index.htm";
Document doc = Jsoup.connect(redirectURL).timeout(0).get();


Element body = doc.body();
Elements es = body.select("p");
//Elements es2 = body.select("span[onmouseover]");
//Elements es3 = body.select("input[name=time_01]");
//String ii=es3.toString();
//String sm=ii.substring(54,64);
//System.out.println(sm);
// fileWriter.write(String.valueOf(sm));
//fileWriter.write("\r\n");

Iterator it = es.iterator();
String[]s=new String[100];
int i=0;
while( it .hasNext() )
{
Element e = (Element) it.next();
s[i]=e.text();
System.out.println(s[i]);
i++;

}
}
然后我把writefile()方法放到if (redirectLocation != null)里面,运行结果是我没有登陆只是这个writefile()的结果是那样的,这是为什么?我明明是在前面程序登陆上后才执行的这个方法,只不过从新连接了一下redirectURL
就是这个代码Document doc = Jsoup.connect(redirectURL).timeout(0).get();
可能就是这里的问题,但是我要获得doc就必须这个做,才能进行信息筛选,小弟才疏学浅,如有别的方法求赐教


如果是我的话我会直接把网页抓下来用正则提取我需要的内容。。


按照你的方法,那么我很想知道你的这个doc是否获取到了数据呢?
Jsoup我没用过,不知道是导入的包还是你自己封装的工具类。所以也没法判断你连接是否成功,
你觉得是这里的问题的话,那么是否取到了网页的代码呢?
或者你可以简单的输出一下doc,看看是否为Null。。

[解决办法]
引用:
Quote: 引用:

你前面一定有:
import org.apache.commons.httpclient.Header或
import org.apache.commons.httpclient.*

改成:
import org.apache.http.Header
就行了,或者:
org.apache.http.Header locationHeader = response.getFirstHeader("Location");

这是运行结果的一部分是红字为什么?
九月 06, 2013 9:51:18 上午 org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected: "[version: 0][name: t][value: 08c932d7b8e9bed3b229e02850c648fc][domain: renren.com][path: /xtalk/][expiry: null]". Illegal path attribute "/xtalk/". Path of origin: "/PLogin.do"
还有进去后的内容为什么是html源文件,而不是页面显示的那种文本信息,小弟是想学这个方法,所以想先保证正确再学,希望大神帮忙解释一下

抓取的页面当然是html源文件,httpClient模拟浏览器去访问目标网页,与浏览器得到的是一样的,如果要获取文本信息,需要对返回内容进行解析,可以用htmlParser等工具包来解析。

读书人网 >Java相关

热点推荐