• 请不要在回答技术问题时复制粘贴 AI 生成的内容
goyiyi
V2EX  ›  程序员

Java 如何匹配 html 文本

  •  1
     
  •   goyiyi · Mar 12, 2020 · 3084 views
    This topic created in 2295 days ago, the information mentioned may be changed or developed.
    <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 replies    2020-03-13 14:37:53 +08:00
    mgcnrx11
        1
    mgcnrx11  
       Mar 12, 2020 via iPhone
    jsoup 解析后读出
    wujieyuan
        2
    wujieyuan  
       Mar 12, 2020
    <a href=\"(.*?)\" class
    EminemW
        3
    EminemW  
       Mar 12, 2020
    jsoup
    goyiyi
        4
    goyiyi  
    OP
       Mar 12, 2020
    @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  
       Mar 12, 2020
    ^ 表示前面没有字符,,你的 html 中 空格也是字符啊
    HuHui
        6
    HuHui  
       Mar 12, 2020 via Android
    好好看看文档
    goyiyi
        7
    goyiyi  
    OP
       Mar 12, 2020
    @HuHui 话题终结者,你为什么要回复我的帖子,哈哈
    guolaopi
        8
    guolaopi  
       Mar 12, 2020
    明显 a 后面有 rel 你没匹配啊。。。
    goyiyi
        9
    goyiyi  
    OP
       Mar 12, 2020
    @guolaopi 那个是干扰项,不要的
    guolaopi
        10
    guolaopi  
       Mar 12, 2020
    @goyiyi
    这么说吧,你写这个是匹配 a 空格 href 这样的字符串,但是你给的例子里都是 a 空格 rel=xxx 空格 href
    所以根本匹配不到啊。。。
    aguesuka
        11
    aguesuka  
       Mar 13, 2020 via Android
    html 本质是 xml,用 xpath 语法比较好吧
    T0DD
        12
    T0DD  
       Mar 13, 2020
    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  
       Mar 13, 2020
    @goyiyi 另外,我自己测试过对于较长的 html 字符串(>2000 ),用 regex 做多匹配性能比较差,差不多一次计算要 20 ~ 25ms
    可以尝试用 String.indexOf & String.subString 配合,写一个固定场景下的 extractStrings(), 能有 20 倍左右的性能提升。
    zifangsky
        14
    zifangsky  
       Mar 13, 2020
    用 XPath 解析 +1
    lu5je0
        15
    lu5je0  
       Mar 13, 2020
    href="(.*?/)"
    goyiyi
        16
    goyiyi  
    OP
       Mar 13, 2020
    @T0DD useful
    lithium4010
        17
    lithium4010  
       Mar 13, 2020
    xpath
    lithium4010
        18
    lithium4010  
       Mar 13, 2020
    jsoup
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5411 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 06:02 · PVG 14:02 · LAX 23:02 · JFK 02:02
    ♥ Do have faith in what you're doing.