【发布时间】:2020-07-01 16:44:37
【问题描述】:
我在尝试获取数据并在 ReactFullpage 组件中呈现时遇到了一些问题。
错误提示:TypeError: Cannot read property '0' of null
由于在附加的脚本中,他将从行中获取数据,这里是 var url = https://www.amazon.de/dp/B07YD776RP?ref=myi_title_dp 的示例
// script for scraping amazon data outgoing from an url
function import_amazon_data() {
//go to Google Sheet & get new income form column 4
var scraperSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_seller_data")
var lrow = scraperSheet.getLastRow();
for(var i=2;i<=lrow;i++){
var url = scraperSheet.getRange(i, 4).getValue()
var regExprice = /<span id="priceblock_ourprice.*<\/span>/gi
var regExsellername = /id=['"]sellerProfileTriggerId['"]>.*?<\/a>/gi
var regEximgsrc = /data-old-hires=".*?"/gi
var getContent = UrlFetchApp.fetch(url).getContentText().trim();
//match of HTML elements with fetched URL
var price = getContent.match(regExprice)
price = price[0]
price = price.replace('<span id="priceblock_ourprice" class="a-size-medium a-color-price priceBlockBuyingPriceString">',"")
.replace('</span>',"")
scraperSheet.getRange(i, 6).setValue(price)
var sellername = getContent.match(regExsellername)
sellername = sellername[0]
sellername = sellername.replace("id='sellerProfileTriggerId'>","")
.replace('id="sellerProfileTriggerId">',"")
.replace('<\/a>',"")
scraperSheet.getRange(i, 7).setValue(sellername)
var imgsrc = getContent.match(regEximgsrc)
imgsrc = imgsrc[0]
imgsrc = imgsrc.replace('data-old-hires="',"")
.replace('"',"")
scraperSheet.getRange(i, 5).setValue(imgsrc)
}
}
【问题讨论】:
-
哪一行给出了错误?
-
错误出现在第 24 行。我已经看到,亚马逊将他的跨度 ID 更改为: 67,99 € 但即使我更改了注册。在我的脚本中的表达式,它仍然无法在获取的 url 中找到价格。也许我对 var regExprice 的表达式做错了什么?这是现在的样子: var regExprice = /
The Error is on line 24.如果我将您的代码粘贴到编辑器中,则没有部分尝试访问第 24 行上变量的元素[0]。行是什么?编码?我运行它时没有错误,所以没有你的工作表我无法模仿你的设置。您能否please provide all necessary information 重现您的情况,否则我们无法提供帮助。这里是我的谷歌驱动器的链接:drive.google.com/drive/folders/…请更改共享设置,因为它目前无法查看。
标签: javascript google-apps-script