PB取网页内容的方法

本文介绍了一种通过网络爬虫技术批量获取大学特定年级、院系课程成绩的方法,包括设置URL、参数构造、发送HTTP请求、解析响应数据并提取有用信息。程序能够根据输入的年级和院系代码,自动爬取对应课程的成绩信息。
以前以为PB不能做此类程序,今天在网上看到,赶紧收起来转发到BLOG上,以后可能用得上
 string ls_get_url, ls_args
string ls_response_text, ls_status_text
long   ll_status_code
long ps1, ps2, ps3, ps4
string srstr1, srstr2
n_cst_string lhv_string
OleObject loo_xmlhttp
string ls_nj, ls_deptno, ls_deptname, ls_spenme, ls_temp1, ls_temp2, ls_temp3
long isel_total=0, row, row_spe, lii, lij, li_rs, li_rsdet

ls_nj trim(ddlb_1.text)
if ls_nj='' then
    messageboxx('提示','请先选择年级!', Information!, OK!, 1)
    return
end if
if dw_dept.rowcount()<=0 then return

ls_get_url "http://" trim(sle_1.text)+"/newcsu/common/search/classresult1.aspx"

dw_special.reset()

for row=1 to dw_dept.rowcount()
    if dw_dept.getitemnumber(row,'isel')=1 then
        isel_total++
        ls_deptno trim(dw_dept.getitemstring(row,'院系代码'))
        ls_deptname trim(dw_dept.getitemstring(row,'院系名称'))
        //ls_args "grade=06级%20&deptId=03%20&deptName=资源加工与生物工程学院"
        ls_args "grade="+ls_nj+"%20&deptId="+ls_deptno+"%20&deptName="+ls_deptname
        
        try
          loo_xmlhttp CREATE oleobject
          loo_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.4.0")
          loo_xmlhttp.open ("GET",ls_get_url "?" ls_args, false)
          loo_xmlhttp.send()
          //Get our response
          ls_status_text loo_xmlhttp.StatusText
          ll_status_code loo_xmlhttp.Status
          //Check HTTP Response code for errors
          if ll_status_code >= 300 then
            messageboxx('提示','Web的GET请求失败!~r~n~r~n'+ls_response_text, Information!, OK!, 1)
          else
             //Get the response we received from the web server
             ls_response_text loo_xmlhttp.ResponseText
          end if
          
          srstr1 ls_response_text
          srstr2 '~t</tr><tr style="color:#003399;background-color:White;">~r~n~t~t<td align="Center">'
          ps1  pos(ls_response_text, srstr2, 1)
          if ps1=0 then continue
          srstr1 mid(ls_response_text, ps1+len(srstr2))
          srstr1 lhv_string.of_globalreplace(srstr1,srstr2, '')
          srstr1 lhv_string.of_globalreplace(srstr1,'</td><td align="Center">', '~t')
          srstr1 lhv_string.of_globalreplace(srstr1,'</td><td>', '~t')
          srstr1 lhv_string.of_globalreplace(srstr1,'</td>', '')
          srstr1 lhv_string.of_globalreplace(srstr1,'~t</tr>~r~n</table>~r~n~t~t~t</form>~r~n~t</body>~r~n</HTML>~r~n','')
          //mle_1.text srstr1

          //Done so cleanup
          loo_xmlhttp.DisconnectObject()
    
          li_rs long( f_fj_str(srstr1,'~r~n',0))
          for lii=1 to li_rs
                ls_temp1 f_fj_str(srstr1,'~r~n',lii )
                ls_temp2 f_fj_str(ls_temp1, '~t', )
                ls_spenme ls_temp2
                ls_temp2 f_fj_str(ls_temp1, '~t', )
                ls_temp2 mid(ls_temp2,2) '/'
                li_rsdet long(f_fj_str(ls_temp2,'/',0))
                for lij=1 to li_rsdet
                    ls_temp3 trim (f_fj_str(ls_temp2,'/',lij))
                    row_spe dw_special.insertrow(0)
                    dw_special.setitem(row_spe,'isel', 1)
                    dw_special.setitem(row_spe,'zy', ls_spenme)
                    dw_special.setitem(row_spe,'bj', ls_temp3)
                    dw_special.setitem(row_spe,'yx', ls_deptname)
                next
          next        
          if dw_special.rowcount()>0 then
             cb_getxs.enabled true
           else
            cb_getxs.enabled false
          end if
        
        catch (RuntimeError rte)
            messageboxx('运行时错', rte.getMessage(), Information!, OK!, 1)
            return
        end try
        
    end if
next

if isel_total=0 then
    messageboxx('提示','请先选择院系!', Information!, OK!, 1)
    return
end if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值