Firstly, the 2nd line of your function is incorrectly formatted, use this instead:
const url = "https://www.bricklink.com/catalogPG.asp?P=" + PartID + "&ColorID=" + ColorID;
updated code should look like this:
function BLprice(PartID,ColorID) {
const url = "https://www.bricklink.com/catalogPG.asp?P=" + PartID + "&ColorID=" + ColorID;
const html = UrlFetchApp.fetch(url).getContentText();
const regex = /Avg Price:<\/TD><TD><B>GBP ([\d\.]+)/;
const match = regex.exec(html);
if (match && match[1]) {
return match[1].trim();
} else {
return "N/A";
}
}
This should match and return the "0.05" price you want.
However, it seems your big problem is that the website is blocking requests from Google servers which is why =IMPORTXML/+IMPORTHTML don't seem to work either. I can get the code to work locally but not as a Google Apps Script.
One possible solution would be to setup a Google Colab python notebook that is connected to your Google Sheet to get the data you need, it won't be a UDF that you had setup but does allow you to programmatically get the data you need, here is the equivalent code that allows Colab to output to Sheets:
import requests
import gspread
from google.auth import default
from google.colab import auth
from bs4 import BeautifulSoup
# Authenticate and create the Google Sheets client
auth.authenticate_user()
creds, _ = default()
client = gspread.authorize(creds)
# Open the Google Sheet and select the first worksheet
sheet_id = '<your_sheet_id_>' #in the url of the sheet after https://docs.google.com/spreadsheets/d/
sheet = client.open_by_key(sheet_id)
worksheet = sheet.sheet1
# Define the function to fetch the price data
def fetch_price_data(part_id, color_id):
url = f'https://www.bricklink.com/catalogPG.asp?P={part_id}&ColorID={color_id}'
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"}
resp = requests.get(url,headers=headers)
print(resp)
soup = BeautifulSoup(resp.content, 'html.parser')
avg_price_tag = soup.find('td', string='Avg Price:')
avg_price_value = avg_price_tag.find_next_sibling('td').text.strip()
print(avg_price_value)
return avg_price_value
# Define the list of part IDs and color IDs to fetch data for
part_ids = ['98138']
color_ids = ['11']
# Loop through the part IDs and color IDs and update the Google Sheet
for i in range(len(part_ids)):
part_id = part_ids[i]
color_id = color_ids[i]
avg_price = fetch_price_data(part_id, color_id)
print(part_id,color_id,avg_price)
worksheet.update_cell(i+1, 1, part_id)
worksheet.update_cell(i+1, 2, color_id)
worksheet.update_cell(i+1, 3, avg_price)