由于PPBC的防盗链,从其他网站打开图片链接:
http://img.plantphoto.cn/image2/b/351969.jpg
你不会看到图片的真是内容,此时需要设置referer
当尝试添加referer之后还是没用。
后来发现使用的是xmlhttp对象,而xmlhttp设置referer是无效的,于是改用winhttp对象,成功。
Sub test()
For i = 1 To 10
a = "http://www.plantphoto.cn"
a2 = Cells(i, 1).Value 'http://img.plantphoto.cn/image2/b/351969.jpg
Set ie = CreateObject("WinHttp.WinHttpRequest.5.1")
ie.Open "GET", a2, False
ie.setRequestHeader "Referer", a
ie.Send
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.write ie.Responsebody
.savetofile ThisWorkbook.Path & "\" & i & ".jpg", 2
.Close
End With
'Set ie = Nothing
Next
MsgBox "over"
End Sub