我正在使用Apify从json文件链接获取数据。这是json数据:
{"exhibitor-single":[{"company":"020-EPOS GmbH","street":"Kruppstraße 74","street2":"",
"zip":"45145","city":"Essen","country":"Germany","title":"","firstname":"Ines","lastname":"Konwiarz",
"position":"Projektleitung","title2":"","firstname2":"","lastname2":"","position2":"",
"phone":"+49 201 1515102","fax":"","email":"[email protected]",
"url":"http://www.020epos.de","products":[],"exhibitorboothnumber":"2-431",
"locationname":"hall 2","eventareaname":""}]}
因此,我在apify webscraper任务中使用了以下代码。
async function pageFunction(context) {
const request = context.request;
const $ = context.jQuery;
var data = $('body > pre').html();
var items = JSON.parse(data);
return {
Url: request.url,
Last_Name: items.lastname,
First_Name: items[`exhibitor-single`].firstname,
Email: items.email
};
}
变量data
具有用于json数据的正确的CSS选择器。但是,它不返回任何数据。谁能帮我找到问题所在?预先感谢。
从pageFunction结构,我想您正在使用apify/web-scraper。
[如果只想从JSON链接中获取数据,则可以轻松使用apify/cheerio-scraper。由于您不需要打开整个浏览器,因此它的计算能力会大大降低。
您需要在cheerio scraper中使用setup pageFunction来获取JSON数据:pageFunction:
async function pageFunction(context) {
const { request ,json } = context;
const items = json;
return {
Url: request.url,
Last_Name: items.lastname,
First_Name: items[`exhibitor-single`].firstname,
Email: items.email
};
}
Cheerio scraper默认情况下仅支持HTML响应,您需要使用值更新高级配置中的其他mime类型:
[
"application/json"
]