中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-06-14 19:03
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 求助,如何在网页源文件中提取图片地址?
楼 主 求助,如何在网页源文件中提取图片地址? 发表于 2007-01-22 06:35 ·  中国 江苏 南京 电信
高级用户
★★
积分 623
发帖 214
注册 2006-09-22 20:48
UID 63387
状态 离线
我想从下面的网页源文件中把所有图片的地址提取出来放到一文件tu.txt里.一行个地址,如:
http://www.pcpop.com/pp/images/pp4_r3_c3.gif
http://www.pcpop.com/pp/images/pp4_r3_c3.gif
http://www.pcpop.com/pp/images/pp4_r3_c3.gif
,图片类型为jpg,gif,图片链接在源文件里无固定位置的.只提取http开头的完整的图片地址.
网页源文件下载:
p.txt.
你也可以随便找个有图片的网页把它的源文件拿来试验,不要vbs版的.
网页源文件一部分为:
<TR align=middle>
<TD align=left width=306>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD width="7%"><IMG height=25 src="http://www.pcpop.com/pp/images/pp4_r3_c3.gif" width=21 border=0></TD>
<TD vAlign=bottom width="90%" background=images/pp4_r3_c10.gif>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=ffffff_14 align=left height=22><STRONG><A class=ffffff_14 title=美女风情 href="http://www.pcpop.com/pp/t007400212_12512_1.html" target=_blank>美女风情</A></STRONG></TD></TR></TBODY></TABLE></TD>
<TD width="3%"><A href="http://www.pcpop.com/pp/t007400212_12512_1.html" target=_blank><IMG height=25 src="http://www.pcpop.com/pp/images/pp4_r3_c12.gif" width=51 border=0></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD height=1></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD bgColor=#ffffff height=2></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD align=middle bgColor=#f2f2f2>
<TABLE cellSpacing=0 cellPadding=0 width=101 border=0>
<TBODY>
<TR>
<TD vAlign=top align=middle width=101 background=images/pp1_6.jpg height=132>
<TABLE cellSpacing=0 cellPadding=0 width=20 border=0>
<TBODY>
<TR>
<TD height=4></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=right><A href="http://union.soqii.com/page/01/" target=_blank><IMG height=120 src="http://img2.pcpop.com/ArticleImages/0x0/0/386/000386225.jpg" width=90 border=0></A></TD>
<TD width=6></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=middle bgColor=#ffffff height=40><A class=x434343 href="http://union.soqii.com/page/01/" target=_blank>即将发生的诱惑</A><BR><A class=x434343 href="http://union.soqii.com/page/01/" target=_blank>(632P)</A></TD></TR></TBODY></TABLE></TD>
<TD width=20 background=images/pp3_4.gif>&nbsp;</TD>
<TD align=middle bgColor=#f2f2f2>
<TABLE cellSpacing=0 cellPadding=0 width=101 border=0>
<TBODY>
<TR>
<TD vAlign=top align=middle width=101 background=images/pp1_6.jpg height=132>
<TABLE cellSpacing=0 cellPadding=0 width=20 border=0>
<TBODY>
<TR>
<TD height=4></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=right><A href="http://www.huzhai.com/bbs/" target=_blank><IMG height=120 src="http://img2.pcpop.com/ArticleImages/0x0/0/332/000332157.jpg" width=90 border=0></A></TD>
<TD width=6></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=middle bgColor=#ffffff height=40><A class=x434343 href="http://www.huzhai.com/bbs/" target=_blank>偷拍对面楼里的</A><BR><A class=x434343 href="http://www.huzhai.com/bbs/" target=_blank>(354P)</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD height=2></TD></TR></TBODY></TABLE></TD>
<TD align=right width=306>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>
<TD width="7%"><IMG height=25 src="http://www.pcpop.com/pp/images/pp4_r3_c3.gif" width=21 border=0></TD>
<TD vAlign=bottom width="90%" background=images/pp4_r3_c10.gif>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=ffffff_14 align=left height=22><STRONG><A class=ffffff_14 title=婷婷玉立 href="http://www.pcpop.com/pp/t007400212_11894_1.html" target=_blank>婷婷玉立</A></STRONG></TD></TR></TBODY></TABLE></TD>
<TD width="3%"><A href="http://www.pcpop.com/pp/t007400212_11894_1.html" target=_blank><IMG height=25 src="http://www.pcpop.com/pp/images/pp4_r3_c12.gif" width=51 border=0></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="99%" border=0>
<TBODY>
<TR>

[ Last edited by tianzizhi on 2007-1-22 at 06:44 AM ]
QQ:419503077
2 发表于 2007-01-22 06:54 ·  中国 湖北 武汉 电信
版主
★★★★★
积分 11,386
发帖 4,938
注册 2006-07-23 17:10
UID 59080
状态 离线

  这个利用sed的正则匹配应该比较好解决。
3 发表于 2007-01-22 07:23 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
积分 5,226
发帖 1,737
注册 2006-03-10 00:38
UID 51697
来自 成都
状态 离线
  贴一段纯批处理的代码出来:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('findstr "http://.*gif" test.txt') do (
set "var=%%i"
set "var=!var: src=☆!"
set "var=!var:.gif"=☆!"
set "var=!var:*☆=!"
for /f "tokens=1 delims==☆" %%j in ("!var!") do echo %%~j.gif
)
pause
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
4 发表于 2007-01-22 07:41 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
积分 788
发帖 366
注册 2006-12-31 02:43
UID 75048
性别 男
状态 离线
html标记语言貌似对行要求放的很松,也就是所有的代码可以写在同一行上面,同一行出现两个用scr=引导的图片地址也不为奇,这时namejm的代码好像只能找到第一个吧
对网页的语言了解不多,不知有没有记错……
5 发表于 2007-01-22 07:44 ·  中国 江苏 南京 电信
高级用户
★★
积分 623
发帖 214
注册 2006-09-22 20:48
UID 63387
状态 离线
thanks very much!!!!
QQ:419503077
6 发表于 2007-01-22 08:15 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
积分 4,103
发帖 1,744
注册 2006-01-20 13:00
UID 49241
性别 男
来自 甘肃.临泽
状态 离线
sed "/http/s/.*\(http:\/\/.*\.gif\).*/\1/;/^http:/!d" test.txt
7 发表于 2007-01-22 08:43 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
积分 5,226
发帖 1,737
注册 2006-03-10 00:38
UID 51697
来自 成都
状态 离线
  若同一行上可能存在几个gif链接,那就使用下面的代码吧(为了兼容连接符&,结果添加了引号,仍然会过滤掉感叹):

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('findstr "http://.*gif" test.txt') do (
set "var=%%i"
set "var=!var:"=!"
set "var=!var: src=☆!"
set "var=!var:.gif=★!"
call :pick-up "!var!"
)
pause
goto :eof

:pick-up
set "var=%var:*☆=%"
for /f "tokens=1 delims==★" %%j in ("%var%") do echo "%%j.gif"
set "str_tmp=%var:☆=%"
if not "%str_tmp%"=="%var%" goto pick-up
goto :eof
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
8 发表于 2007-02-04 00:13 ·  美国 缅因州
中级用户
★★
积分 316
发帖 152
注册 2006-06-18 13:01
UID 57204
性别 男
状态 离线
注意到楼主这句话-------图片类型为jpg,gif

@sed "/jpg\|gif/!d;s/.*src=\x22\(*\)\x22.*/\1/;/^http/!d" p.txt> temp.txt

基于GNU sed 4.1.4

[ Last edited by amao on 2007-2-4 at 10:59 AM ]
9 发表于 2007-05-08 11:10 ·  中国 广东 中山 电信
初级用户
★★
积分 127
发帖 62
注册 2007-04-19 12:43
UID 85744
性别 男
状态 离线
学到东西! '<'并没有处理好
论坛跳转: