write excel vba code to https://proff.no/ put in a company name in the search field then…
- User
write excel vba code to https://proff.no/ put in a company name in the search field then return the first Org nr from the output
- Assistant
write excel vba code to https://proff.no/ put in a company name in the search field then return the first Org nr from the output
Certainly! Below is an example of Excel VBA code that searches for a company name on proff.no, retrieves the search results, and extracts the first "Org nr" (Organization number) from the output.
Important Notes Before You Begin:
ALT + F11.Tools > References.Sub GetFirstOrgNr()
Dim companyName As String
Dim searchURL As String
Dim httpRequest As MSXML2.XMLHTTP60
Dim htmlDoc As New HTMLDocument
Dim orgNrElement As HTMLDivElement
Dim orgNr As String
' Get the company name from cell A1 (or specify it directly)
companyName = Range("A1").Value ' Or set companyName = "Your Company Name"
' URL-encode the company name
companyName = URLEncode(companyName)
' Build the search URL
searchURL = "https://www.proff.no/sok?q=" & companyName
' Create an HTTP request
Set httpRequest = New MSXML2.XMLHTTP60
httpRequest.Open "GET", searchURL, False
httpRequest.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
httpRequest.send
' Load the response into an HTMLDocument
htmlDoc.body.innerHTML = httpRequest.responseText
' Find the first Org nr in the search results
' You may need to adjust the CSS selector based on the actual HTML structure
Set orgNrElement = htmlDoc.querySelector(".search-results .search-result .search-result__organization-number")
If Not orgNrElement Is Nothing Then
orgNr = orgNrElement.innerText
' Output the Org nr to cell B1
Range("B1").Value = orgNr
MsgBox "First Org nr: " & orgNr
Else
MsgBox "Org nr not found."
End If
End Sub
' Function to URL-encode the company name
Function URLEncode(ByVal Text As String) As String
Dim i As Long
Dim CharCode As Integer
Dim EncodedText As String
For i = 1 To Len(Text)
CharCode = Asc(Mid$(Text, i, 1))
Select Case CharCode
Case 48 To 57, 65 To 90, 97 To 122
' Alphanumeric characters
EncodedText = EncodedText & Mid$(Text, i, 1)
Case Else
EncodedText = EncodedText & "%" & Format$(Hex(CharCode), "00")
End Select
Next i
URLEncode = EncodedText
End Function
Get the Company Name: The macro retrieves the company name from cell A1 in Excel. You can change this to get the company name from another cell or specify it directly in the code.
URL Encoding: The URLEncode function ensures that the company name is properly encoded to be included in a URL.
Build the Search URL: It constructs the search URL by appending the encoded company name as a query parameter.
HTTP Request:
XMLHTTP object to send an HTTP GET request to the search URL.User-Agent header is set to mimic a regular browser request.Load HTML Response: The response text (HTML content) is loaded into an HTMLDocument object for parsing.
Parse the HTML:
querySelector method to find the first element that contains the Org nr..search-results .search-result .search-result__organization-number is hypothetical and may need to be adjusted based on the actual HTML structure of proff.no.Output the Org nr:
B1 in Excel.The success of the code depends on correctly targeting the HTML elements that contain the Org nr. To adjust the code:
Inspect the Website:
Update the CSS Selector:
querySelector string in the code to match the HTML structure you've observed.For example, if the Org nr is inside a <span> element with the class org-no, within a <div> with the class search-result, you might use:
Set orgNrElement = htmlDoc.querySelector(".search-result .org-no")
If the website uses JavaScript to load content after the page has initially loaded (AJAX), the XMLHTTP method might not fetch the dynamically loaded Org nr. In such cases, you can use Selenium with VBA to control a real browser that can execute JavaScript.
Prerequisites for Selenium:
VBA Code Using Selenium:
Sub GetFirstOrgNr_Selenium()
Dim driver As New Selenium.ChromeDriver
Dim companyName As String
Dim searchURL As String
Dim orgNrElement As WebElement
Dim orgNr As String
' Get the company name from cell A1
companyName = Range("A1").Value
' URL-encode the company name
companyName = URLEncode(companyName)
' Build the search URL
searchURL = "https://www.proff.no/sok?q=" & companyName
' Start Chrome browser
driver.Start "Chrome" ' You can also specify Firefox or another browser
driver.Get searchURL
' Wait for the page to load
driver.Wait 5000 ' Waits for 5 seconds; adjust as needed
' Find the Org nr element using XPath or CSS Selector
' Adjust the selector based on the actual HTML structure
Set orgNrElement = driver.FindElementByCss(".search-result .org-no")
If Not orgNrElement Is Nothing Then
orgNr = orgNrElement.Text
Range("B1").Value = orgNr
MsgBox "First Org nr: " & orgNr
Else
MsgBox "Org nr not found."
End If
' Close the browser
driver.Quit
End Sub
Notes:
driver.Wait time or use explicit waits to ensure that the page has fully loaded before attempting to find elements.XMLHTTP: Suitable for static pages where content is available in the initial HTML response.Always ensure your scraping activities comply with the website's terms of service and any applicable laws. Be respectful of the website's resources and consider implementing pauses or rate limits if scraping large amounts of data.
Let me know if you need further assistance or clarification on any of these steps!
Not yet included in a published release.