RSS Feed

网络词霸

2006-06-15 by bborn

大概两个月前 帮别人有偿做了一个软件
通过网络查询英文单词
可以查询英语单词和汉语词汇
界面做的很简单
原理大概就是用过网络查询
之后获得网页 从网页中提取有用的信息
稍微麻烦的就是网页有三个
要从第一个网页中得到另外两个网址
然后再获取 获取的方法是用通常的对字符串的处理
而不是正则表达式 发现这个在处理这种类型的问题时候很不灵活
效率也差些
之前先有人说服务器那边会对查询来路进行
因而用软件不能查询 并用java写了例程
其实这个很好解决
可能他是用”oop”用惯了

CString getweb(CString word)
{
CString output;

CInternetSession session(“MySession”);
CHttpConnection* conn = NULL;
CHttpFile* file = NULL;

conn = session.GetHttpConnection(“www.dreye.com.cn”, (INTERNET_PORT)8080);

CString strPost = word;

file = conn->OpenRequest(CHttpConnection::HTTP_VERB_POST,_T(“/axis/ddict.jsp”));
file->AddRequestHeaders(“Content-Type: application/x-www-form-urlencoded”);
file->AddRequestHeaders(“Referer: http://www.dreye.com.cn/”); //服务器识别来路
file->AddRequestHeaders(“Accept-Language : zh-cn”);
file->SendRequest(NULL,0,(LPVOID)(LPSTR)(LPCTSTR)strPost,strPost.GetLength());
CString strRead;

while(file->ReadString(strRead))
{
output+=strRead;
}

return output;

}

呵呵 看到是哪个网站了吧
从网页中提取信息的代码

CString formatweb(CString html)
{
CString output;

html.Replace(”

html.Replace(”
“,”\r\n”);

int start=html.Find(”

“);
if (start==-1)
{
start=html.Find(”

“);
}
int end=html.Find(“

“);

bool ifskip = true;
for(int i=start;i
{
if(html.GetAt(i)==’<’)
ifskip=true;
if(html.GetAt(i)==’>’)
{
ifskip=false;
continue;
}
if(ifskip)
continue;

output+=html.GetAt(i);
}
return output;

}

思路就是先找到信息的开头和结尾
把中间的换行替换为”\r\n”
之后再过滤掉html标签中的东西就是了
不过需要说明的是三个网页信息的提取并不是一样的
有个还是需要小小改动一下的 道理是一样的

主要代码就是这样
其实那网站想要封掉可以很简单的更换样式表就可以
这个当时花了我大概一天是时间去弄他
还是挺好玩的

可能相关


没有评论 »

  1. hao 说道:

    Good job!能写一个类似于google分析页面的程序吗?

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">