• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Web scraping zip code

YasserKhalil

Well-Known Member
Hello everyone
This website "https://www.unitedstateszipcodes.org" enables to search for addresses to get zip code
I would put address in the search box .. example :"180 Bevis Lane, Statesville, NC" and click search .. and the zip code would be as main header in bold like that "ZIP Code 28677"
How could I extract the 28677 ?
 
Hi!! Run the below script and get the result in Range B2.
Code:
Sub zipcode_scraper()

Dim http As New XMLHTTP60, html As New HTMLDocument
Dim elem As Object, argstr As String

argstr = "q=180+Bevis+Lane%2C+Statesville%2C+NC"

With http
    .Open "POST", "https://www.unitedstateszipcodes.org/", False
    .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    .send argstr
    html.body.innerHTML = .responsetext
End With

Set elem = html.getElementById("map-info").getElementsByTagName("h1")
Range("B2") = Replace(elem(0).innerText, "ZIP Code ", "")

End Sub

Make sure to add "Microsoft xml" and "Microsoft HTML object library" in the reference library before you run it.
 
Last edited:
Back
Top