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

[文件操作] 批处理文件如何从杂乱的网页源代码总提取所需图片地址并保存?

[复制链接]
发表于 2012-1-15 15:29:14 | 显示全部楼层 |阅读模式
如何从杂乱的网页源代码总提取所需图片地址并保存?
我这有某网站的源代码文件1.txt~100.txt,一下是1.txt的其中的一部分:
  1. href="http://www.topit.me/item/2277171">12964667477319</a></div><div class="info"></div></div><a href="http://www.topit.me/item/2277171"><img id="item_d_2277171" class="img" alt="12964667477319" width="245px" height="200px" src="http://img.topit.me/m/201101/31/12964667477319.jpg" /></a></div><div class="e m"><div class="hover bar"><div class="heartbtn"><a id="item_heart_771744" class="heart nologin" href="http://www.topit.me/login?ref=%2Ftag%2FComic%3Fp%3D1"><span>heart</span></a></div><div class="title"><a href="http://www.topit.me/item/771744">12416993</a></div><div class="info"></div></div><a href="http://www.topit.me/item/771744"><img id="item_d_771744" class="img" alt="12416993" width="245px" height="200px" src="http://img.topit.me/m/201008/14/12817636911240.jpg" /></a></div><div class="e m"><div class="hover bar"><div class="heartbtn"><a id="item_heart_533918" class="heart nologin" href="http://www.topit.me/login?ref=%2Ftag%2FComic%3Fp%3D1"><span>heart</span></a></div><div class="title"><a href="http://www.topit.me/item/533918">Ⅶ</a></div><div class="info"></div></div><a href="http://www.topit.me/item/533918"><img id="item_d_533918" class="img" alt="Ⅶ" width="245px" height="200px" src="" /></a></div><div class="e m"><div class="hover bar"><div class="heartbtn"><a id="item_heart_2324230"
复制代码
我需要提取整个文件中所有以类似:
  1. http://img.topit.me/m/201008/14/12817636911240.jpg
复制代码
格式的图片地址(有些地址可能不在同一行中)保存到到List.txt文件中,效果如下:
  1. http://img.topit.me/m/201101/31/12964667477319.jpg
  2. http://img.topit.me/m/201008/14/12817636911240.jpg
  3. http://img.topit.me/m/201007/14/12791168666293.jpg


复制代码
同时我在论坛里找到了荣誉版主namejm大大的类似功能的代码,但是却不能解决我的问题,希望大家修改一下:
  1. @echo off
  2. cd.>list.txt
  3. for %%i in (*.htm) do (
  4.     (echo.&echo %%i 中的图片&echo.)>>list.txt
  5.     for /f "delims=" %%j in ('findstr /i "src=.*http://.*\.jpg" %%i 2^>nul') do (
  6.         set "str=%%j"
  7.         setlocal enabledelayedexpansion
  8.         set str=!str:"=!
  9.         set str=!str:*src=!
  10.         for /f "delims==> " %%k in ("!str!") do echo %%k>>list.txt
  11.         endlocal
  12.     )
  13. )
  14. start list.txt
复制代码
发表于 2012-1-15 17:49:24 | 显示全部楼层
因为等号是默认分隔符,所以可以用这个特点以 for %%b 来对网页原文件进行划分
  1. @echo off
  2. for /f "delims=" %%a in (1.txt) do (
  3.    for %%b in (%%a) do (
  4.       for /f delims^=^" %%c in ("%%b") do if /i %%~xc==.jpg echo %%c
  5.    )
  6. )
  7. pause
复制代码

评分

参与人数 1技术 +1 收起 理由
ivor + 1 学习了

查看全部评分

发表于 2012-1-15 18:58:12 | 显示全部楼层
  1. grep -Po "http[^\x22]*[0-9]+.jpg" url
复制代码
发表于 2012-1-15 21:40:43 | 显示全部楼层
正则表达式啊,findstr啊
 楼主| 发表于 2012-1-16 19:34:36 | 显示全部楼层
:):):):)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-20 04:12 , Processed in 0.022636 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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