Use ASP to automatically parse image addresses in webpages

Source: Internet
Author: User

1. Obtain the image address on the original page.

<%
Function picstr (STR)
Set objregexp = new Regexp 'to set the configuration object
Objregexp. ignorecase = true' case insensitive
Objregexp. Global = true' is set to full-text search.
Objregexp. pattern = " "'To ensure that the image address can be accurately retrieved, there are two layers of configuration: First, find the label in it, then, the getimgs function behind the image address is used to implement the next function.
STRs = trim (STR)
Set matches = objregexp. Execute (STRs )'
For each match in matches
Picstr = picstr & getimgs (match. Value )'
Next
'All the images are in this src = "http: // image address", so you can obtain the exact image address in this way.
End Function

Function getimgs (STR)
Getimgs = ""
Set objregexp1 = new Regexp
Objregexp1.ignorecase = true
Objregexp1.global = true
Objregexp1.pattern = "http: //. +? "'Retrieve the address
Set Mm = objregexp1.execute (STR)
For each mattings in mm
Getimgs = getimgs & "|" & left (match1.value, Len (match1.value)-1) 'Concatenates the addresses in the string for backup.
Next
End Function
%>

2. Download the image and save it on the server.

<%
Function gethttppage (URL)
On Error resume next
Dim HTTP
Set HTTP = server. Createobject ("msxml2.xmlhttp") 'uses the XMLHTTP method to obtain the image content
HTTP. Open "get", URL, false
HTTP. Send ()
If HTTP. readystate <> 4 then
Exit Function
End if
Gethttppage = http. responsebody
Set HTTP = nothing
If err. Number <> 0 then err. Clear
End Function
'The content obtained from the image must be saved. It gives you the feeling that you can use FSO for the image, but it doesn't actually work. Program An error occurs. Because FSO does not support streaming files, we need to call another object: Ado. strem. The specific process is as follows:
Function saveimage (from, tofile)
Dim geturl, objstream, IMGs
Geturl = trim (from)
IMGs = gethttppage (geturl) 'The process of obtaining the rest content of an image
Set objstream = server. Createobject ("ADODB. Stream") 'creates an ADODB. Stream object, which must be of version ADO 2.5 or later.
Objstream. type = 1' open in binary mode
Objstream. Open
Objstream. Write IMGs write string content to buffer
Objstream. savetofile server. mappath (tofile), 2'-Write the buffered content to the file
Objstream. Close () 'Close the object
Set objstream = nothing
End Function

'The process is as follows:
Arrimg = Split (picstr (STR), "|") 'splits the string to obtain the address list.
Allimg = ""
Newimg = ""
For I = 1 to ubound (arrimg)
If arrimg (I) <> "" And instr (allimg, arrimg (I) <1 then ', check whether the image has been downloaded.
Fname = baseurl & CSTR (I & Mid (arrimg (I), Rev (arrimg (I ),".")))
Saveimage (arrimg (I), fname) 'specifies the function for saving the address. For details about the process, see the preceding section.
Allimg = allimg & "|" & arrimg (I) 'returns the address string of the saved image to determine the address to be replaced.
Newimg = newimg & "|" & fname' returns the local address string
End if
Next
'Step 3 is to replace the original address. The specific process is as follows:
Arrnew = Split (newimg, "|") 'to obtain the original image address list
Arrall = Split (allimg, "|") 'obtains the address list of saved images.
For I = 1 to ubound (arrnew) ', execute the loop to replace the original address
STRs = Replace (STRs, arrall (I), arrnew (I ))
Next
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.