d语言-下载网页,通过正则抓取对应的数据


---我的博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow me

std.net.curl

d语言的网路curl库,提供更高级的服务。

依赖 libcurl。[std.net.curl模块详细介绍地址](https://dlang.org/phobos/std_net_curl.html)

依赖于对libcurl的封装,提供一系列的高级方法哦。

download  upload  get  post  put  del  options  trace  connect  byLine  byChunk  byLineAsync  byChunkAsync 


我们这里用到了get

std.regex

d语言的正则库

match matchAll等


std.conv 

可以使用!to(xxx)方法 用于类型转换

代码如下

import std.net.curl, std.stdio;
import std.regex;
import std.conv;

void main() {
    //fech data
    auto url = "http://novel.hongxiu.com/a/263958/";
    auto content = cast(string) get(url);
    //regex data
    auto  r = regex(r"http://w.hongxiu.com/(\d+)/index.html");  
    auto match_arr = matchFirst(content,r);
    int s_bid = 0;
    if(!match_arr.empty){
        auto s_b = match_arr[1];
        s_bid = to!int(s_b);
    }   
    writeln(s_bid);
}


你可能感兴趣的:(编程语言,D,匹配数据)