:( http://lsg.cnki.net/grid20/Brief.aspx?ID=1&classtype=&systemno=&NaviDatabaseName=&NaviField=
这个是中国期刊网的检索请求,我现在需要通过这个来获取检索出来的信息,解决方法已经有了,就是得不到检索信息,查询内容是空的。
应该是少add了一些检索所需内容。
下面是我写的代码:
[code="java"]
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
public class PostTest {
public static void main(String[] args){
PostMethod postMethod = new PostMethod("http://lsg.cnki.net/grid20/Brief.aspx?ID=1&classtype=&systemno=&NaviDatabaseName=&NaviField=");
postMethod.addParameter("advancedvalue1","java");
postMethod.addParameter("advancedfield1","主题");
postMethod.addParameter("yearstart","1979");
postMethod.addParameter("yearend",String.valueOf(GregorianCalendar.getInstance().get(Calendar.YEAR)));
postMethod.addParameter("RecordsPerPage","20");
postMethod.addParameter("SearchRange","All");
postMethod.addParameter("searchmatch","0");
postMethod.addParameter("order","dec");
postMethod.addParameter("display","chinese");
postMethod.addParameter("NaviDatabaseName","ZJCLS");
postMethod.addParameter("encode","gb");
postMethod.addParameter("VarNum","1");
postMethod.addParameter("singleleafcode","J");
postMethod.addParameter("strNavigatorName",",A,B,C,D,E,F,G,H,I,J");
postMethod.addParameter("strNavigatorValue",",理工A(数学物理力学天地生),理工B(化学化工冶金环境矿业),理工C(机电航空交通水利建筑能源),农业,医药卫生,文史哲,政治军事与法律,教育与社会科学综合,电子技术及信息科学,经济与管理");
HttpClient client = new HttpClient();
client.setConnectionTimeout(1000 * 60);
int status=0;
try {
status = client.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(status==HttpStatus.SC_OK) {
makeResponseObject(postMethod.getResponseBodyAsString());
} else {
}
postMethod.releaseConnection();
}
// 根据HTTP请求后返回的字串生成结果封装类的实例
private static void makeResponseObject(String sResponse) {
System.out.println("-------------------"+sResponse);
System.out.println("-------------------");
String regExNums = "
}
[/code]
这里应该有高手把,希望别让我失望,先谢谢大家了。
[b]问题补充:[/b]
一楼的你和没说一样。。你随便看一眼也知道是post。
你请求的页面有两个form,左边一个,右边一个,你递交的是右边的form查询。左边的条件没有传过去,也就是分类信息没有传过去,分类信息是必须要有的。
这位大哥,谢谢了。。我知道分类信息,现在就是不知道怎么加这个分类信息,你能帮我看看吗?
貌似中国期刊网不让用httpclient抓数据,我把它页面上所有hidden的值都传过去了都抓不到.
[code="java"]
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class PostTest {
public static void main(String[] args) {
new PostTest().executeMethod();
}
private void executeMethod() {
PostMethod postMethod = initPostMethod();
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(1000 * 60);
int status = 0;
try {
status = client.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (status == HttpStatus.SC_OK) {
try {
makeResponseObject(postMethod.getResponseBodyAsString());
} catch (IOException e) {
e.printStackTrace();
//To change body of catch statement use File | Settings | File Templates.
}
}
postMethod.releaseConnection();
}
private PostMethod initPostMethod() {
PostMethod postMethod = new PostMethod("http://lsg.cnki.net/grid20/Brief.aspx?ID=1&classtype=&systemno=&NaviDatabaseName=&NaviField=");
postMethod.addParameter("advancedvalue1", "java");
postMethod.addParameter("advancedfield1", "主题");
postMethod.addParameter("yearstart", "1979");
postMethod.addParameter("yearend", String.valueOf(GregorianCalendar.getInstance().get(Calendar.YEAR)));
postMethod.addParameter("RecordsPerPage", "20");
postMethod.addParameter("SearchRange", "All");
postMethod.addParameter("searchmatch", "0");
postMethod.addParameter("order", "dec");
postMethod.addParameter("NaviDatabaseName", "ZJCLS");
// postMethod.addParameter("singleleafcode", "J");
postMethod.addParameter("strNavigatorValue", ",A,B,C,D,E,F,G,H,I,J");
postMethod.addParameter("strNavigatorName", ",理工A(数学物理力学天地生),理工B(化学化工冶金环境矿业),理工C(机电航空交通水利建筑能源),农业,医药卫生,文史哲,政治军事与法律,教育与社会科学综合,电子技术及信息科学,经济与管理");
postMethod.addParameter("selectbox", "I");
//hidden files
postMethod.addParameter("ID", "1");
postMethod.addParameter("hdnIsAll", "false");
postMethod.addParameter("SearchQueryID", "7");
postMethod.addParameter("SearchFieldRelationDirectory", "主题/[],篇名/[],题名/[],作者&英文作者/[SYS_Author_Relevant],关键词[],第一责任人/[SYS_Author_Relevant],机构/[SYS_Organization_Relevant],摘要/[],引文/[],基金/[SYS_Fund_Relevant],全文/[]");
postMethod.addParameter("updateTempDB", "CJFDTEMP,CJFDYTMP");
postMethod.addParameter("updateTempDB", "CJFDTEMP,CJFDYTMP");
postMethod.addParameter("bCurYearTempDB", "1");
postMethod.addParameter("fieldnowordfrequency", "关键词,作者,第一责任人,机构,中文刊名&英文刊名,年',期',基金,分类号',ISSN',CN',DOI'");
postMethod.addParameter("TableType", "PY");
postMethod.addParameter("display", "chinese");
postMethod.addParameter("encode", "gb");
postMethod.addParameter("TablePrefix", "CJFD");
postMethod.addParameter("View", "中国期刊全文数据库");
postMethod.addParameter("yearFieldName", "年");
postMethod.addParameter("VarNum", "1");
postMethod.addParameter("__VIEWSTATE", "dDwtMzQyNzY1MjMwO3Q8O2w8aTwwPjtpPDM+O2k8Nj47PjtsPHQ8cDxsPFRleHQ7PjtsPOS4reWbveacn+WIiuWFqOaWh+aVsOaNruW6kzs+Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPFw8c2NyaXB0IGxhbmd1YWdlPSJqYXZhc2NyaXB0Ilw+DQpmdW5jdGlvbiBjaGVjaygpDQp7DQoJdHJ5DQoJew0KCQlSZWFsQ2hlY2soJ0knLCdaSkNMUycpXDsNCgl9DQoJY2F0Y2goZXJyKQ0KCXsNCgl9DQp9DQpcPC9zY3JpcHRcPg0KOz4+Oz47Oz47dDxwPGw8VGV4dDs+O2w85qOA57Si5a+86IiqOz4+Ozs+Oz4+Oz7zTeDfZ7WSvqyHxy/vAVM4gIxmAw==");
return postMethod;
}
// 根据HTTP请求后返回的字串生成结果封装类的实例
private void makeResponseObject(String sResponse) {
System.out.println("-------------------" + sResponse);
System.out.println("-------------------");
String regExNums = "< TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*COLOR:\\s*#000000;\\s*WORD-WRAP:\\s*break -word\"\\s*vAlign=center\\s*bgColor=#f4f5f6\\s*height=30>\\s*共有记录\\s* (\\d+)\\s*条\\s* \\s*</TD>";
String regExData = "<TR>\\s*<TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*WORD-WRAP:\\s*break-word\"\\s*(?:align=left)?\\s*bgColor=#\\S+\\s*>([\\s\\S]*?)</TR>";
String regExPage = "< TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*COLOR:\\s*#000000;\\s*WORD-WRAP:\\s*break -word\"\\s*vAlign=center\\s*bgColor=#f4f5f6\\s*height=30>\\s*<A\\s*href =\"(.*)\">(?:上页|下页)</A>";
}
}
[/code]
分类信息是这个postMethod.addParameter("selectbox", "I");
关键在于这个__VIEWSTATE值,我传了之后会就会Redirect requested but followRedirects is disabled,不传拿不到数据。
你确定服务器端接受的是post方法而不是get方法吗?
你最好把网站上标准的查询form中的input整个对照一下,包括,还有textarea,也记录下。
form中的method是get还是post?
你现在要做的就是模仿这个post动作。
如果还是不行,就设置httprequest的header,加入 refferer,设定为那个网站自己,因为有些网站禁止盗链,一般是利用refferer判断。
你请求的页面有两个form,左边一个,右边一个,你递交的是右边的form查询。左边的条件没有传过去,也就是分类信息没有传过去,分类信息是必须要有的。