Google Sheets Apps 脚本,基于零件 ID 和颜色 ID 从 Bricklink 抓取价格的代码

问题描述 投票:0回答:1

我正在尝试在 Google 表格中创建一个自定义函数,让我获取“新”的 6 个月平均价格,该函数让我输入零件 ID 和颜色 ID。

例如下面的url中,98138是零件ID,11是颜色ID

https://www.bricklink.com/catalogPG.asp?P=98138&ColorID=11

我希望函数 =BLprice(98138,11) 从表中的部分输出 $0.05。 (Here's the part of the table I want to grab.)

这是我用来测试它的谷歌表格的链接: https://docs.google.com/spreadsheets/d/1N_3jKiSfi-ENfoah4fziFf815-hTm0tbz0U_snvw6BI/edit?usp=sharing

我现在的代码是

函数 BLprice(PartID,ColorID) {

const url = "https://www.bricklink.com/catalogPG.asp?P=%22&PartID&%22&ColorID="&ColorID;

const html = UrlFetchApp.fetch(url).getContentText();

return html.match(/AVG Price:(.*)/)[1].trim();

}

我基于以下内容(而且我对编码非常缺乏经验):

Google Sheet 使用 Importxml 错误 could not fetch url

问题:我想学习如何使用 google apps 脚本将数字从网站导入到 google 表格

当前错误是“Exception: Bad request: http://0 (line 3).” 在尝试解决这个问题时,我还遇到了无法获取 url 的错误(那是在尝试执行 importxml 并尝试获取表的一部分时)

xml google-apps-script google-sheets web-scraping
1个回答
0
投票

首先,函数的第二行格式不正确,请改用:

  const url = "https://www.bricklink.com/catalogPG.asp?P=" + PartID + "&ColorID=" + ColorID;

更新后的代码应该是这样的:

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&nbsp;([\d\.]+)/;
  const match = regex.exec(html);
  if (match && match[1]) {
    return match[1].trim();
  } else {
    return "N/A";
  }
}

这应该匹配并返回您想要的“0.05”价格。

但是,您的大问题似乎是该网站阻止了来自 Google 服务器的请求,这就是为什么 =IMPORTXML/+IMPORTHTML 似乎也不起作用的原因。我可以让代码在本地运行,但不能作为 Google Apps 脚本运行。

一个可能的解决方案是设置一个连接到您的 Google 表格的 Google Colab python 笔记本以获取您需要的数据,它不会是您设置的 UDF,但允许您以编程方式获取所需的数据,这是允许 Colab 输出到表格的等效代码:

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)
© www.soinside.com 2019 - 2024. All rights reserved.