您的位置:程序门 -> vb -> 网络编程



如何抓取网页上的图片


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何抓取网页上的图片[已结贴,结贴人:hwmfly]
发表于:2007-03-27 09:40:11 楼主
如题,给出思路即可,谢谢
发表于:2007-03-27 09:58:291楼 得分:8
1、创建internetexplorer.application对象,并得到document对象
2、使用html文档对象解析document对象,得到图片的url
3、使用urlmon   api直接下载图片即可
发表于:2007-03-27 19:13:062楼 得分:1
不懂,学习来了,帮顶吧
你能给我一分就不错了,谢谢!
发表于:2007-03-27 19:40:463楼 得分:0
谢谢   wocoolbila()   的回复,但是我觉得既然webbrowser已经显示出图片来了,没有必要再去下载了,因为图片已经在本地机子上了,应该有方法直接把webbrowser显示的图片保存下来
发表于:2007-03-28 00:34:374楼 得分:20
for   each   wx   in   web.document.all
                if   wx.tagname   =   "img "   then
                      if   wx.src= "http://www.11111.com/1111.bmp "   then
                                set   ctrlrange   =   web.document.body.createcontrolrange()
                                ctrlrange.add   (wx)
                                ctrlrange.EXECcommand   ( "copy ")
                                savepicture   clipboard.getdata,   "c:\1.bmp "
                                exit   for
                        end   if
                end   if
        next
发表于:2007-03-28 06:26:055楼 得分:10
use   urldownloadtofile
it   will   check   local   cache   first
发表于:2007-04-04 11:15:526楼 得分:1
up
发表于:2007-04-10 18:47:157楼 得分:30
收藏一下
发表于:2007-04-10 22:28:558楼 得分:0
mark,是截图还是下载文件?
发表于:2007-04-10 23:53:339楼 得分:0
直接用   winsock   去下载图片,收到的数据为2进制流,保存文件就是你要的图片了
发表于:2007-04-12 10:02:2110楼 得分:0
hotus的答案正解
发表于:2007-04-13 10:57:4211楼 得分:0
我用了   hotus()   的方法,确实可行,但是又出来一个新问题,怎么判断图片已加载完毕
因为我用document_compelete   事件不能判断图片加载完毕
我是用vb   2005写的,已经另开贴子讨论这个问题了,
http://community.csdn.net/expert/topic/5462/5462536.xml?temp=.4668848
发表于:2007-04-13 22:35:1112楼 得分:5
这种方法是可用的,但用到了剪贴板,若你在用电脑或其他软件用到剪贴板,会有冲突的呀,如何解决?!!
发表于:2007-04-14 14:30:0513楼 得分:0
我用了这种方法,在用电脑的时候很不好用!!
发表于:2007-04-14 17:42:3014楼 得分:20
1、搜索url
2、判断是否在缓存,文件路径多少
3、在缓存拷贝,不在缓存下载

1、hotus()   的方法
2、网上可以搜索。记得vbgood好像有那个代码
3、   urldownloadtofile   这个是调用ie的下载方式
发表于:2007-04-20 10:06:2215楼 得分:0
option   explicit

'wininet访问网络api
private   declare   function   internetopen   lib   "wininet.dll "   alias   "internetopena "   (byval   sagent   as   string,   byval   laccesstype   as   long,   byval   sproxyname   as   string,   byval   sproxybypass   as   string,   byval   lflags   as   long)   as   long
private   declare   function   internetconnect   lib   "wininet.dll "   alias   "internetconnecta "   (byval   hinternetsession   as   long,   byval   lpszservername   as   string,   byval   nproxyport   as   integer,   byval   lpszusername   as   string,   byval   lpszpassword   as   string,   byval   dwservice   as   long,   byval   dwflags   as   long,   byval   dwcontext   as   long)   as   long
private   declare   function   internetopenurl   lib   "wininet.dll "   alias   "internetopenurla "   (byval   hinternet   as   long,   byval   lpszurl   as   string,   byval   lpszheaders   as   string,   byval   dwheaderslength   as   long,   byval   dwflags   as   long,   byval   dwcontext   as   long)   as   long
private   declare   function   httpopenrequest   lib   "wininet.dll "   alias   "httpopenrequesta "   (byval   hhttpsession   as   long,   byval   sverb   as   string,   byval   sobjectname   as   string,   byval   sversion   as   string,   byval   sreferer   as   string,   byval   something   as   long,   byval   lflags   as   long,   byval   lcontext   as   long)   as   long
private   declare   function   httpsendrequest   lib   "wininet.dll "   alias   "httpsendrequesta "   (byval   hhttprequest   as   long,   byval   sheaders   as   string,   byval   lheaderslength   as   long,   byval   soptional   as   string,   byval   loptionallength   as   long)   as   integer
private   declare   function   httpqueryinfo   lib   "wininet.dll "   alias   "httpqueryinfoa "   (byval   hhttprequest   as   long,   byval   linfolevel   as   long,   byref   sbuffer   as   any,   byref   lbufferlength   as   long,   byref   lindex   as   long)   as   integer
private   declare   function   httpaddrequestheaders   lib   "wininet.dll "   alias   "httpaddrequestheadersa "   (byval   hhttprequest   as   long,   byval   sheaders   as   string,   byval   lheaderslength   as   long,   byval   lmodifiers   as   long)   as   integer
private   declare   function   internetreadfile   lib   "wininet.dll "   (byval   hfile   as   long,   byval   sbuffer   as   string,   byval   lnumbytestoread   as   long,   lnumberofbytesread   as   long)   as   integer
private   declare   function   internetreadfilebyte   lib   "wininet.dll "   alias   "internetreadfile "   (byval   hfile   as   long,   byref   sbuffer   as   byte,   byval   lnumbytestoread   as   long,   lnumberofbytesread   as   long)   as   integer
private   declare   function   internetclosehandle   lib   "wininet.dll "   (byval   hinet   as   long)   as   integer

private   const   internet_flag_no_cache_write   =   &h4000000
private   const   http_query_content_length   =   5
private   const   internet_open_type_preconfig   =   0
private   const   internet_invalid_port_number   =   0
private   const   internet_flag_reload   =   &h80000000
private   const   internet_service_http   =   3
private   const   internet_default_http_port   =   80
private   const   http_addreq_flag_add   =   &h20000000
private   const   http_addreq_flag_replace   =   &h80000000

private   const   useragent   =   "mozilla/4.0   (ulickbot(www.ulick.net);   msie   6.0;   windows   nt   5.1) "
private   hnet   as   long
private   hconnect   as   long
private   hrequest   as   long
private   hurlfile   as   long
private   bret   as   long

private   sub   class_initialize()
        hnet   =   internetopen(useragent,   internet_open_type_preconfig,   vbnullstring,   internet_invalid_port_number,   0)
end   sub

private   sub   class_terminate()
        if   hrequest   then   internetclosehandle   hrequest
        if   hconnect   then   internetclosehandle   hconnect
        if   hnet   then   internetclosehandle   hnet
end   sub

'发送url
public   function   sendurl(byval   surl   as   string)   as   long
        'on   error   resume   next
        dim   hurl   as   long
        hurl   =   internetopenurl(hnet,   surl,   vbnullstring,   0,   internet_flag_reload,   0)
        sendurl   =   hurl
end   function

'发送url,返回这个url文件的html代码,可选referer参数
public   function   geturlfile(byval   surl   as   string,   optional   sreferer   as   string)   as   string
        'on   error   resume   next
        dim   dwsize   as   long
        dim   htmlbody   as   string
        dim   dwbuf   as   string   *   1024
        if   len(sreferer)   then
                hurlfile   =   sendreferer(surl,   sreferer)
        else
                hurlfile   =   sendurl(surl)
        end   if
        if   hurlfile   =   0   then   exit   function
        do
                dwbuf   =   vbnullstring
                bret   =   internetreadfile(hurlfile,   dwbuf,   1024,   dwsize)
                htmlbody   =   htmlbody   &   mid(dwbuf,   1,   dwsize)
        loop   while   (dwsize   <>   0)
        if   hurlfile   then   internetclosehandle   hurlfile
        geturlfile   =   htmlbody
end   function

public   function   filedownload(byval   surl   as   string,   byval   sfile   as   string,   optional   sreferer   as   string)   as   boolean
        dim   b(999)   as   byte
        dim   endbyte()   as   byte
        dim   bdoloop   as   boolean
        dim   sreadbuffer   as   string
        dim   lnumberofbytesread   as   long
        dim   fileid   as   integer
        dim   strsize   as   string
        dim   size   as   long
        strsize   =   string$(1024,   "   ")
        fileid   =   freefile
        if   len(sreferer)   then
                hurlfile   =   sendreferer(surl,   sreferer)
        else
                hurlfile   =   sendurl(surl)
        end   if
        httpqueryinfo   hurlfile,   http_query_content_length   or   internet_invalid_port_number,   byval   strsize,   len(strsize),   0
        size   =   clng(trim(strsize))
        if   size   =   0   then   exit   function
        open   sfile   for   output   as   fileid
        close   fileid
        open   sfile   for   binary   as   fileid
        dim   j   as   long
        for   j   =   1   to   size   \   1000
                bdoloop   =   internetreadfilebyte(hurlfile,   b(0),   1000,   lnumberofbytesread)
                put   fileid,   ,   b
                if   not   cbool(lnumberofbytesread)   then   exit   for
        next
        if   size   mod   1000   <>   0   then
                dim   tmp   as   long
                tmp   =   (size   mod   1000)   -   1
                redim   endbyte(tmp)
                bdoloop   =   internetreadfilebyte(hurlfile,   endbyte(0),   tmp   +   1,   lnumberofbytesread)
                put   fileid,   ,   endbyte
        end   if
        close   fileid
        if   hurlfile   then   internetclosehandle   hurlfile
        filedownload   =   true
end   function

private   function   sendreferer(byval   surl   as   string,   byval   sreferer   as   string)   as   long
        dim   srv,   url,   sheader   as   string
        dim   i   as   long
        i   =   instr(surl,   "/ ")
        srv   =   mid(surl,   i   +   2,   len(surl)   -   (i   +   1))
        i   =   instr(srv,   "/ ")
        url   =   mid(srv,   i,   len(srv)   +   1   -   i)
        srv   =   left$(srv,   i   -   1)
        if   hrequest   then   internetclosehandle   hrequest
        if   hconnect   then   internetclosehandle   hconnect
        hconnect   =   internetconnect(hnet,   srv,   0,   vbnullstring,   "http/1.0 ",   internet_service_http,   0,   0)
        if   hconnect   =   0   then   exit   function
        hrequest   =   httpopenrequest(hconnect,   "get ",   url,   "http/1.0 ",   vbnullstring,   0,   internet_flag_reload,   0)
        if   hrequest   =   0   then   exit   function
        sheader   =   "referer: "   &   iif(len(sreferer)   =   0,   surl,   sreferer)   &   vbcrlf
        bret   =   httpaddrequestheaders(hrequest,   sheader,   len(sheader),   http_addreq_flag_replace   or   http_addreq_flag_add)
        bret   =   httpsendrequest(hrequest,   vbnullstring,   0,   vbnullstring,   0)
        sendreferer   =   hrequest
end   function
发表于:2007-04-20 10:08:2416楼 得分:0
用:geturlfile下载网页代码,用正则表答式分析,用:filedownload下载图片。
发表于:2007-04-22 12:03:1417楼 得分:5
如果知道图片路径,
private   declare   function   urldownloadtofile   lib   "urlmon "   alias   "urldownloadtofilea "   (byval   pcaller   as   long,   byval   szurl   as   string,   byval   szfilename   as   string,   byval   dwreserved   as   long,   byval   lpfncb   as   long)   as   long

public   function   downfile(url   as   string,   localfilename   as   string)   as   boolean   '下载文件
dim   lngretval   as   long
lngretval   =   urldownloadtofile(0,   url,   localfilename,   0,   0)
if   lngretval   =   0   then
downloadfile   =   true
else
debug.print   "downloadfile   error "
end   if
end   function

如果是图片存在缓存中,那么就是直接从缓存中下载
发表于:2007-04-22 13:27:5218楼 得分:0
分析网页源码来获取图片路径


快速检索

最新资讯
热门点击