V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
goyiyi
V2EX  ›  程序员

Java 如何匹配 html 文本

  •  1
     
  •   goyiyi · 2020-03-12 21:34:59 +08:00 · 2105 次点击
    这是一个创建于 1497 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <div class="col-12 col-md-6 col-xl-4 provider hide"> 
              	<a rel="nofollow" href="/company/zade-servers/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
             </div> 
             <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zadeservers"> 
              	<a href="/company/zade-servers/" class="dont-style-link"> </a> 
             </div> 
             <div class="col-12 col-md-6 col-xl-4 provider hide"> 
              	<a rel="nofollow" href="/company/zappie-host-llc/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
             </div> 
             <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zappiehostllc"> 
              	<a href="/company/zappie-host-llc/" class="dont-style-link">  </a> 
             </div> 
             <div class="col-12 col-md-6 col-xl-4 provider hide"> 
              	<a rel="nofollow" href="/company/zare/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
             </div> 
             <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zare"> 
              	<a href="/company/zare/" class="dont-style-link">  </a> 
             </div> 
             
             更多省略了 ...  ... 
    
    

    java 如何匹配其中的 /company/zade-servers/ /company/zappie-host-llc/ /company/zare/ 字符串,然后把他们装进 list<string></string>

    我写的规则:"^<a href="\/company\/.*\/$"

    结果:无法匹配出内容,希望懂正则的 v 友帮忙看看

    18 条回复    2020-03-13 14:37:53 +08:00
    mgcnrx11
        1
    mgcnrx11  
       2020-03-12 21:43:52 +08:00 via iPhone
    jsoup 解析后读出
    wujieyuan
        2
    wujieyuan  
       2020-03-12 21:48:44 +08:00
    <a href=\"(.*?)\" class
    EminemW
        3
    EminemW  
       2020-03-12 21:49:05 +08:00
    jsoup
    goyiyi
        4
    goyiyi  
    OP
       2020-03-12 21:58:11 +08:00
    @mgcnrx11
    @EminemW jsoup 的 Document 好像只能拿到节点 多个 Element,还是需要去匹配拿 href 里的内容,主要是拿超链接节点的时候,还存在干扰项,我不知道该怎么排除它:<a rel="nofollow noopener" href="/company/zade-servers/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a>
    heyjei
        5
    heyjei  
       2020-03-12 22:06:32 +08:00
    ^ 表示前面没有字符,,你的 html 中 空格也是字符啊
    HuHui
        6
    HuHui  
       2020-03-12 22:10:27 +08:00 via Android
    好好看看文档
    goyiyi
        7
    goyiyi  
    OP
       2020-03-12 22:41:35 +08:00
    @HuHui 话题终结者,你为什么要回复我的帖子,哈哈
    guolaopi
        8
    guolaopi  
       2020-03-12 22:45:57 +08:00
    明显 a 后面有 rel 你没匹配啊。。。
    goyiyi
        9
    goyiyi  
    OP
       2020-03-12 22:48:04 +08:00
    @guolaopi 那个是干扰项,不要的
    guolaopi
        10
    guolaopi  
       2020-03-12 23:34:26 +08:00
    @goyiyi
    这么说吧,你写这个是匹配 a 空格 href 这样的字符串,但是你给的例子里都是 a 空格 rel=xxx 空格 href
    所以根本匹配不到啊。。。
    aguesuka
        11
    aguesuka  
       2020-03-13 00:14:45 +08:00 via Android
    html 本质是 xml,用 xpath 语法比较好吧
    T0DD
        12
    T0DD  
       2020-03-13 00:39:15 +08:00
    String reg = "a href=\"/company/.+?/";
    Pattern p = Pattern.compile(reg);
    Matcher m = p.matcher(a); // a is your original string.
    List<String> resultList = new LinkedList<>();
    while (m.find()) {
    String str = m.group();
    str = "<string>" + str.replace("a href=\"", "") + "</string>";
    resultList.add(str);
    }


    output:
    [<string>/company/zade-servers/</string>,
    <string>/company/zappie-host-llc/</string>,
    <string>/company/zare/</string>]


    @goyiyi 匹配多个的话,不能用贪婪模式
    T0DD
        13
    T0DD  
       2020-03-13 00:44:19 +08:00
    @goyiyi 另外,我自己测试过对于较长的 html 字符串(>2000 ),用 regex 做多匹配性能比较差,差不多一次计算要 20 ~ 25ms
    可以尝试用 String.indexOf & String.subString 配合,写一个固定场景下的 extractStrings(), 能有 20 倍左右的性能提升。
    zifangsky
        14
    zifangsky  
       2020-03-13 09:11:18 +08:00
    用 XPath 解析 +1
    lu5je0
        15
    lu5je0  
       2020-03-13 10:13:20 +08:00
    href="(.*?/)"
    goyiyi
        16
    goyiyi  
    OP
       2020-03-13 12:59:18 +08:00
    @T0DD useful
    lithium4010
        17
    lithium4010  
       2020-03-13 14:36:42 +08:00
    xpath
    lithium4010
        18
    lithium4010  
       2020-03-13 14:37:53 +08:00
    jsoup
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3487 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:43 · PVG 08:43 · LAX 17:43 · JFK 20:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.