找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 22210|回复: 12

[文本处理] 【已解决】批处理如何从文本里提取.html链接并输出成txt(链接消重并单独成列)

[复制链接]
发表于 2023-12-29 10:07:09 | 显示全部楼层 |阅读模式
RT,假设有一个包含html源码的文件(以.txt的形式),里面包含有很多网页小链接的网址(都是"https"开头+"xx.html"收尾,也可以是"https"开头+"xx.shtml"收尾,可以在源文件里自行定制编辑)的形式,如何提取里面的链接并(消重和列表)输出成txt?

【举例】

处理前

<li class="li3"><div class="txt"><a href="https://www.gamersky.com/news/202312/1690849.shtml" title="查干湖冬捕启幕:“好运头鱼”拍出1399999元" target="_blank" class="countHitList" data-itemid="1136773">查干湖冬捕启幕:“好运头鱼”拍出1399999元</a></div><div class="time">09:53</div></li>

<li class="li3"><div class="txt"><a href="https://www.gamersky.com/news/202312/1690849.shtml" title="查干湖冬捕启幕:“好运头鱼”拍出1399999元" target="_blank" class="countHitList" data-itemid="1136773">查干湖冬捕启幕:“好运头鱼”拍出1399999元</a></div><div class="time">09:53</div></li>
                                                                                                
<li class="li3"><div class="txt"><a href="https://www.gamersky.com/tech/202312/1690862.shtml" title="雷军:小米汽车2024年进入智能驾驶第一梯队" target="_blank" class="countHitList" data-itemid="1136773">雷军:小米汽车2024年进入智能驾驶第一梯队</a></div><div class="time">09:46</div></li>
                                                                                                
<li class="li3"><div class="txt"><a href="https://www.gamersky.com/news/202312/1690755.shtml" title="小狗也是Xbox粉丝?圣诞PS5拆箱搞笑视频引万人观看" target="_blank" class="countHitList" data-itemid="1136773">小狗也是Xbox粉丝?圣诞PS5拆箱搞笑视频引万人观看</a></div><div class="time">09:39</div></li>
                                                      
                                                                                                

处理后(注:第一个输出的链接被消重了)

https://www.gamersky.com/news/202312/1690849.shtml

https://www.gamersky.com/tech/202312/1690862.shtml

https://www.gamersky.com/news/202312/1690755.shtml

测试样本链接:

https://www.123pan.com/s/pbxrVv-9W9Y.html

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2023-12-29 10:45:51 | 显示全部楼层
算是投机取巧吧:
  1. @echo off
  2. chcp 65001
  3. set _".html"=0
  4. set _".shtml"=0
  5. for /f "delims=" %%i in (test.txt) do (
  6.     for %%j in (%%i) do (
  7.         if defined _"%%~xj" echo,%%~j
  8.     )
  9. )
  10. pause
复制代码
发表于 2023-12-29 11:27:24 | 显示全部楼层
本帖最后由 aloha20200628 于 2023-12-29 11:32 编辑


以下代码存为批处理脚本文件。
运行结果:提取链接地址并去重。
假设 test.txt 是utf-8编码,若是简中编码(即记事本的ANSI编码)则删除代码第二行。

  1. @echo off &setlocal enabledelayedexpansion
  2. chcp 65001>nul
  3. cd.>test.new
  4. for /f tokens^=6^delims^=^=^" %%v in (test.txt) do (
  5.         find "%%~v" test.new>nul || echo,%%v>>test.new
  6. )
  7. type test.new
复制代码
 楼主| 发表于 2023-12-29 11:31:51 | 显示全部楼层
本帖最后由 kokoxx 于 2023-12-29 14:58 编辑

回复 3# aloha20200628


    大佬,首先感谢出手,用你的脚本测试 (我更新了test.txt)
结果如下
/>
1
https://m.downkuai.com/android/163334.html
stylesheet
https://www.downkuai.com/soft_sort.html

searchTop
off
seahotid
Indnav_icon1 indIcon
https://www.downkuai.com/android/list_14.html
Indnav_icon3 indIcon
Indnav_icon4 indIcon
Indnav_icon5 indIcon
Indnav_icon6 indIcon
Indnav_icon7 indIcon
Indnav_icon8 indIcon
dailj
javascript:;
/></div>
target
get_mood('mood1')
nav_tcsap
fl new_pro_img
picAndRank
//img.downkuai.com/logo/20200713/41fdb1a031bf088b65c981cae7e1d479.jpg
//img.downkuai.com/logo/20210630/921c770c692751cd7b3ea8ae56df2445.png
//img.downkuai.com/logo/20210926/49dcd29cd16973f692c2cdca7fc2208b.png
//img.downkuai.com/logo/20220925/f4f357edb64d4ea073ca7c3c96e0c0e4.png
//img.downkuai.com/logo/20231225/9a93e9d490544618a466c2c030be12ae.jpg
//img.downkuai.com/logo/20210706/6ab5e849fbe3d5390187c608281ed469.png
//img.downkuai.com/logo/20210918/0057097ebede97c25c722a6fccd0ffb1.png
//img.downkuai.com/logo/20230627/69c3fc03744b07b1cd81bb6a457ee3a9.jpg
//img.downkuai.com/logo/20230530/1a4b1d014a05f78bb226086eb3c88524.png
clicked
友善发言,温暖评论区
vcode
comment-btn
https://www.downkuai.com/android/48019.html
https://www.downkuai.com/android/110413.html
https://www.downkuai.com/android/124179.html
https://www.downkuai.com/android/109051.html
https://www.downkuai.com/android/110124.html
https://www.downkuai.com/android/110562.html
https://www.downkuai.com/android/66289.html
https://www.downkuai.com/android/59843.html
c_1
c_2
c_3
c_4
c_5
c_6
c_7
c_8
c_9
c_10
c_11
c_12
c_13
c_14
c_15
c_16
c_17
c_18
c_19
c_20
c_21
c_22
c_23
c_24
c_25
c_26
c_27
c_28
c_29
c_30
c_31
c_32
c_33
c_34
c_35
c_36
spealb_img
topsec_xz
_blank
complain1
complain2
complain3
complain4
complain5
complain6
item-1
ceamil
complainTj

也就是有很多不必要的内容 ,而且html与html之间没有做到空行

不过用这个脚本测试我在正文贴出的样本,确实做到了消重
发表于 2023-12-29 11:39:49 | 显示全部楼层
回复 4# kokoxx
为啥坛主总是强调要提供真实完整的样本....
 楼主| 发表于 2023-12-29 11:43:50 | 显示全部楼层
回复 5# aloha20200628


    是我的错
发表于 2023-12-29 14:08:37 | 显示全部楼层
本帖最后由 WHY 于 2023-12-29 14:17 编辑
  1. @echo off
  2. PowerShell "$s = (gc test.txt) -join ''; [regex]::Matches($s, 'https?://(?:(?!\x22|https?://).)+\.s?html(?=\x22)') | forEach {$_.Value} | sort -Unique | sc xxx.txt"
  3. pause
复制代码
 楼主| 发表于 2023-12-29 14:16:56 | 显示全部楼层
回复 7# WHY


    运行后,啥反应都没有
发表于 2023-12-29 14:26:21 | 显示全部楼层
回复 8# kokoxx


    你是啥系统,你是怎么运行脚本的?
像你这种回答,如果我打算继续帮你,你想想看,怎么找线索?
 楼主| 发表于 2023-12-29 14:31:32 | 显示全部楼层
回复 9# WHY


    系统是win10ltsc 小鱼儿版

将。ps1脚本拖入powershell窗口回车后

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\Administrator> H:\新建文件夹\power-link.ps1H:\新建文件夹\power-link.ps1
H:\新建文件夹\power-link.ps1H:\新建文件夹\power-link.ps1 : 无法将“H:\新建文件夹\power-link.ps1H:\新建文件夹\power-link
.ps1”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ H:\新建文件夹\power-link.ps1H:\新建文件夹\power-link.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (H:\新建文件夹\power-...\power-link.ps1:String) [], CommandNotFoundExcept
ion
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\Administrator>
 楼主| 发表于 2023-12-29 14:41:09 | 显示全部楼层
本帖最后由 kokoxx 于 2023-12-29 14:42 编辑

回复 9# WHY


    这下成功了,原来是保存为bat而不是.ps1

不过阁下的脚本还有一个问题,那就是连接与连接之间没有空行

再者,可能的话,能不能将执行完成的文本提示改成“已经处理完毕”而不是“按任意键继续”?
发表于 2023-12-29 14:51:52 | 显示全部楼层
  1. @echo off
  2. PowerShell "$s = (gc test.txt) -join ''; [regex]::Matches($s, 'https?://(?:(?!\x22|https?://).)+\.s?html(?=\x22)') | forEach {$_.Value + "`r`n"} | sort -Unique | sc xxx.txt"
  3. echo;Done
  4. pause > nul
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

 楼主| 发表于 2023-12-29 14:58:27 | 显示全部楼层
回复 12# WHY


    测试通过,感谢大佬出手
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 06:35 , Processed in 0.012670 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表