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

Java 如何匹配 html 文本

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