gpt4 book ai didi

php - 从邮政编码查找完整地址,解决方案是什么?

转载 作者:行者123 更新时间:2023-12-04 03:08:50 25 4
gpt4 key购买 nike

基本上,我正在为我的网站制作申请表。我需要使用用户输入的邮政编码搜索完整地址,并希望提供该邮政编码的结果供他们选择。我知道需要某种数据库,但我正在努力寻找它,希望得到任何帮助。

最佳答案

在英国,您可以使用 192.com 从任何邮政编码获得完整地址。

他们在 192.com 上有一个完全免费的数据库

我不为他们或他们的任何广告商工作,但已将此网站用于许多数据输入应用程序。

根据您正在搜索的邮政编码格式化 URL。即

'a1 1aa' 将是 http://www.192.com/places/a/a1-1/a1-1aa

这将列出邮政编码中的所有地址。

这是我写的类(class),希望对你有帮助:

    Imports System.Net
Imports System.IO

Public Class PCLookup
Property Addresses As List(Of Address)

Public Sub New(Postcode As String)
GetAddresses(CreatDoc(Postcode))
End Sub

Private Function CreatDoc(PostCode As String) As mshtml.HTMLDocument
Dim URL As String = FormatPostcode(PostCode)
If URL = "" Then Return New mshtml.HTMLDocument
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim doc As New mshtml.HTMLDocument
Dim objDoc As mshtml.IHTMLDocument2 = doc
Dim param As Object() = {reader.ReadToEnd()}
objDoc.write(param)
response.Close()
reader.Close()
Return objDoc
End Function

Private Function FormatPostcode(Postcode As String) As String
Dim FullURL As String = "http://www.192.com/places/"
Do Until Postcode.Contains(" ") = False
Postcode = Replace(Postcode, " ", "")
Loop
If Len(Postcode) > 7 Or Len(Postcode) < 5 Then
Return ""
End If
If Len(Postcode) = 5 Then
FullURL &= Mid(Postcode, 1, 1) & "/"
FullURL &= Mid(Postcode, 1, 2) & "-" & Mid(Postcode, 3, 1) & "/"
FullURL &= Mid(Postcode, 1, 2) & "-" & Mid(Postcode, 3) & "/"
End If
If Len(Postcode) = 6 Then
If IsNumeric(Mid(Postcode, 2, 1)) Then
FullURL &= Mid(Postcode, 1, 1) & "/"
FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4, 1) & "/"
FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4) & "/"
Else
FullURL &= Mid(Postcode, 1, 2) & "/"
FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4, 1) & "/"
FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4) & "/"
End If
End If
If Len(Postcode) = 7 Then
FullURL &= Mid(Postcode, 1, 2) & "/"
FullURL &= Mid(Postcode, 1, 4) & "-" & Mid(Postcode, 5, 1) & "/"
FullURL &= Mid(Postcode, 1, 4) & "-" & Mid(Postcode, 5) & "/"
End If
Return FullURL
End Function

Private Sub GetAddresses(ObjDoc As mshtml.HTMLDocument)
Dim Obj As mshtml.IHTMLElementCollection = ObjDoc.getElementsByTagName("td")
Addresses = New List(Of Address)
For Each TD As mshtml.HTMLTableCell In Obj
If TD.className = "address" Then
Dim FullAddress As String = TD.innerText
Addresses.Add(New Address(FullAddress))
End If
Next
End Sub

End Class

Public Class Address
Property Line1 As String
Property Line2 As String
Property Line3 As String
Property Line4 As String
Property Postcode As String
Public Sub New(FullAddress As String)
Dim Obj As Object = Split(FullAddress, ", ")
Select Case UBound(Obj)
Case 4
Line1 = Obj(0) & " " & Obj(1)
Line2 = ""
Line3 = Obj(2)
Line4 = Obj(3)
Postcode = Obj(4)
Case 5
Line1 = Obj(0) & " " & Obj(1)
Line2 = Obj(2)
Line3 = Obj(3)
Line4 = Obj(4)
Postcode = Obj(5)
Case 6
Line1 = Obj(0) & " " & Obj(1)
Line2 = Obj(2) & " " & Obj(3)
Line3 = Obj(4)
Line4 = Obj(5)
Postcode = Obj(6)
End Select

End Sub
End Class

抱歉,如果代码有点乱,自学成才的程序员!

关于php - 从邮政编码查找完整地址,解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7965939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com