ERROR: Webscraping with Puppeteer - Cannot read property 'getProperty' of undefined
ERROR: Webscraping with Puppeteer - Cannot read property 'getProperty' of undefined
我正在尝试抓取 RightMove 中所有返回的搜索项的 URL。页面是:
我正在使用 Puppeteer 尝试执行此操作,但不断收到错误消息“无法读取未定义的 属性 'getProperty'”
这是我的代码:
const puppeteer = require('puppeteer');
async function scrapePropery(URL) {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto(URL);
const [el] = await page.$x('//*[@id="property-105155516"]/div/div/div[4]/div[1]/div[2]/a');
const href = await el.getProperty('href')
const hrefTxt = await href.jsonValue();
console.log({hrefTxt});
} catch (err) {
console.error(err.message);
} finally {
browser.close();
};
}
scrapePropery('https://www.rightmove.co.uk/property-for-sale/find.html?searchType=SALE&locationIdentifier=REGION%5E61299&insId=2&radius=0.0&minPrice=&maxPrice=&minBedrooms=&maxBedrooms=&displayPropertyType=&maxDaysSinceAdded=&_includeSSTC=on&sortByPriceDescending=&primaryDisplayPropertyType=&secondaryDisplayPropertyType=&oldDisplayPropertyType=&oldPrimaryDisplayPropertyType=&newHome=&auction=false');
我做错了什么?我试过抓取不同的元素,但我总是遇到同样的错误,我已经成功地使用了我在其他网站上编写的代码像 Amazon 这样的网站(对 .getProperty 方法中的 属性 进行了细微更改),但 RightMove 不走运。
有什么想法吗?
谢谢
此代码的问题是所使用的 xPath 不正确,这导致了未定义的响应。
我正在尝试抓取 RightMove 中所有返回的搜索项的 URL。页面是:
我正在使用 Puppeteer 尝试执行此操作,但不断收到错误消息“无法读取未定义的 属性 'getProperty'”
这是我的代码:
const puppeteer = require('puppeteer');
async function scrapePropery(URL) {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto(URL);
const [el] = await page.$x('//*[@id="property-105155516"]/div/div/div[4]/div[1]/div[2]/a');
const href = await el.getProperty('href')
const hrefTxt = await href.jsonValue();
console.log({hrefTxt});
} catch (err) {
console.error(err.message);
} finally {
browser.close();
};
}
scrapePropery('https://www.rightmove.co.uk/property-for-sale/find.html?searchType=SALE&locationIdentifier=REGION%5E61299&insId=2&radius=0.0&minPrice=&maxPrice=&minBedrooms=&maxBedrooms=&displayPropertyType=&maxDaysSinceAdded=&_includeSSTC=on&sortByPriceDescending=&primaryDisplayPropertyType=&secondaryDisplayPropertyType=&oldDisplayPropertyType=&oldPrimaryDisplayPropertyType=&newHome=&auction=false');
我做错了什么?我试过抓取不同的元素,但我总是遇到同样的错误,我已经成功地使用了我在其他网站上编写的代码像 Amazon 这样的网站(对 .getProperty 方法中的 属性 进行了细微更改),但 RightMove 不走运。
有什么想法吗?
谢谢
此代码的问题是所使用的 xPath 不正确,这导致了未定义的响应。