怎样获取一个网页上的某个图片的链接地址哇?
我写的是程序功能提取网页上我需要的信息,比如文字,图片和图片所带超链接等信息。现在最头疼的是:怎样获取一个网页上的某个图片的链界地址哇?不是图片地址,而是图片的链接地址(网址) 我都想知道。呵呵 正则表达式?不好意思。我没用过。详细点啊! 普通的模式,如:
<a href="....."><img src=".."></a> 这种获取比较简单了
但还有常用一种,获取就比较困难了,用JS写的
<img src="...." onclick="..." /> 普通的模式,如:
<a href="....."><img src=".."></a> 这种获取比较简单了
怎么弄??? 还是具体问题具体分析吧
弄个网页源码上来看看 用IE库打开网页,然后找到你需要的对象,文字链接是href的属性,下面是我做的从666ccc.com下载音乐的脚本,参考下吧#AutoIt3Wrapper_UseAnsi=n
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <StatusBarConstants.au3>
#include <WindowsConstants.au3>
#include <GuiStatusBar.au3>
Dim $sn
Dim $urls
Dim $item
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 579, 463, 193, 125)
$Input1 = GUICtrlCreateInput("", 8, 32, 450, 21)
$Label1 = GUICtrlCreateLabel("请在下面的文本框中填入地址", 8, 8, 160, 17)
$Button1 = GUICtrlCreateButton("开始获取", 465, 32, 65, 25, 0)
$button2 = GUICtrlCreateButton("下载", 533, 32)
GUICtrlSetState($button2, $GUI_DISABLE)
$ListView1 = GUICtrlCreateListView("序号|歌曲名|地址 ", 8, 64, 561, 361)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
Dim $StatusBar1_PartsWidth =
_GUICtrlStatusBar_SetParts($StatusBar1, $StatusBar1_PartsWidth)
_GUICtrlStatusBar_SetText($StatusBar1, "未开始", 0)
_GUICtrlStatusBar_SetMinHeight($StatusBar1, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
If (GUICtrlRead($Input1) <> "") Then
work()
EndIf
Case $button2
down()
EndSwitch
WEnd
Func error($err)
MsgBox(0, 0, $err)
EndFunc ;==>error
Func work()
$url = GUICtrlRead($Input1)
_GUICtrlStatusBar_SetText($StatusBar1, "正在打开浏览器")
$oIE = _IECreate($url, 0, 0, 1, 0)
$doc = _IEFormGetCollection($oIE, 1)
$elements = _IETagNameGetCollection($doc, "a")
$n = 1
Dim $SongName
$sn = 0;
_GUICtrlStatusBar_SetText($StatusBar1, "正在获取歌曲名信息")
For $i In $elements
If (Mod($n, 6) = 1) Then
$SongName[$sn] = $i.innerHTML
$sn = $sn + 1
EndIf
;### Tidy Error: If/ElseIf statement without a then..
If ($n = 1) Then
$src = $i.href
EndIf
$n = $n + 1
Next
_GUICtrlStatusBar_SetText($StatusBar1, "正在打开播放列表")
$oIE = _IECreate($src, 1, 0, 1, 0)
$mp = _IEGetObjById($oIE, "MediaWrapper")
$object = _IEGetObjById($mp, "MediaPlayer")
$code = $object.innerHTML
$sub = "wma"
$code = StringMid($code, 26)
$url = StringLeft($code, StringInStr($code, Chr(34)) - 1);第一首的地址
$urlcomm = StringTrimRight($url, 6)
_GUICtrlStatusBar_SetText($StatusBar1, "正在生成地址列表")
For $i = 1 To $sn
$urls[$i - 1] = $urlcomm
If ($i < 10) Then
$urls[$i - 1] = $urls[$i - 1] & "0"
EndIf
$urls[$i - 1] = $urls[$i - 1] & $i & ".wma"
Next
_GUICtrlStatusBar_SetText($StatusBar1, "正在将数据加入列表")
For $i = 0 To $sn - 1
$item[$i] = GUICtrlCreateListViewItem(String($i + 1), $ListView1)
GUICtrlSetData($item[$i], "|" & $SongName[$i])
GUICtrlSetData($item[$i], "||" & $urls[$i])
Next
_GUICtrlStatusBar_SetText($StatusBar1, "获取成功,可以点击下载按钮下载")
GUICtrlSetState($button2, $GUI_ENABLE)
EndFunc ;==>work
Func down()
$thunder = ObjCreate("ThunderAgent.Agent")
;AddTask4(URL,注释名称,目录,批量任务时名称,下载页,-1,0,-1,cookie,
For $i = 0 To $sn - 1
$thunder.AddTask4($urls[$i], String($i + 1) & ".wma", "d:\temp", "", "", -1, 0, -1, "", "", "")
Next
$thunder.CommitTasks2(1)
EndFunc ;==>down 如果只想点击链接,不需要管它的链接地址吧,楼主想进行的操作是什么啊?
#include <Ie.au3>
$sURL = "www.baidu.com"
$oIe = ObjCreate("InternetExplorer.Application")
$oIe.Visible = 1
$oIe.Navigate($sURL)
_IELoadWait($oIe)
$oIe.document.images.item(0).focus
$sImgURL= $oIe.document.images.item(0).href
Msgbox(0, $oIe.StatusText, $sImgURL)
先使图片获取焦点,然后读取状态栏上的文字。对于屏蔽掉链接的网页可能不适用。 楼上的方法很强大。。。 javascript:var c = "";var a;for(x=0;x<document.images.length;x++){a = document.images;if(a.parentNode.tagName == "A"){c += "img.src:" + a.src + "\r\n" +"a.href :" +a.parentNode.href + "\r\n\r\n"}}alert(c); 初学,没看懂啊! :face (2): 大部分都看不懂哎还得努力学习 好像有的浏览器有这个功能 可以参考一下
页:
[1]