简体   繁体   English

解析网站数据

[英]Parse data from a website

I am trying to scrap data for personal use from trustpilot co uk. 我正在尝试从trustpilot co uk收集数据以供个人使用。 I parse the name of the websites from this page . 我从此页面解析网站的名称。 The result I get is: 我得到的结果是:

2. Flashbay ,&,;

What I want to get is just the name of the website, eg "Flashbay". 我想要得到的只是网站的名称,例如“ Flashbay”。 I use this command $('.items h3 a') so far. 到目前为止,我使用此命令$('.items h3 a')

Any advice/help of how to accomplish my goal is really appreciated. 非常感谢您提供有关如何实现目标的建议/帮助。

var websites = [];
$.each($('.item h3 a'), function () {
    websites.push($(this).text().trim().replace(/^[^A-Z]+/i, ''));
});
console.log(websites);

This gave me the following: 这给了我以下几点:

["Mr Memory", "GoGoDigital", "Maxram", "Quiet PC.com", "Sell Your-laptop",
 "Wiziwoo Ltd", "OFFTEK", "Memoryc", "Total Computing", "ZiiP GameStore",
 "Comms Express", "MicroDream Limited", "Xytron", "Kikatek", "Gizzmo Heaven",
 "Arbico Computers", "Network Webcams", "ShopTo.Net", "PC Specialist Ltd", "rpm"]

您可以在包含“ 2. Flashbay,&,;”的字符串变量上使用.substring(3,8)。

Using substrings as Damien suggested is probably the simplest route. 如Damien建议的那样使用子字符串可能是最简单的方法。 However, using static indices like that wouldn't work, since the strings you're looking at are of variable length, including the leading text to be trimmed off. 但是,使用这样的静态索引是行不通的,因为您要查看的字符串具有可变长度,包括要修剪的前导文本。 Looking at the page, here's what works for me: 查看页面,这对我有用:

var companies = [];
var elements = $('.items h3 a');
for(var i = 0; i < elements.length;i++){
    var text = $.trim($(elements[i]).text());
    text = text.substr(text.indexOf(".") + 2);
    companies.push(text);
}
console.log(companies);

I've tested that on the page you linked to and it seems to work fine. 我已经在您链接到的页面上对它进行了测试,它似乎可以正常工作。 I don't know where that ,&,; 我不知道在哪里说,&,; you were getting on the end was coming from. 你快要结束了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google 脚本 - 从网站论坛解析 HTML - 并将数据写入工作表 - Google script - parse HTML from Website Forum - and Write Data to Sheet 使用 javascript 从带有 HTTP 请求的网站解析 JSON 数据 - Parse JSON data from a website with HTTP request with javascript CSS选择器代码从棘手的网站上抓取/解析数据 - css selector code to scrape/parse data from tricky website 将数据传递到网站以及从网站传递 - Pass data to website and from website 填写网站数据并点击按钮并解析响应 - Fill website data and click button and parse response 是否可以使用Objective-C从parse.com检索数据并将其显示在网站上? - Is it possible to retrieve data from parse.com using objective-c and show it in website? 如何从需要使用 javascript 登录的网站解析数据? (对于 Pebble 应用程序) - How do I parse data from a website that requires a login using javascript? (For a Pebble app) 我想解析 JSON 数据并将其公开发布在我的本地网站上,来自 API - I would like to parse JSON data and post it publically on my local website, from an API 从网站提取数据 - Data extraction from website 使用cron作业通过我的网站解析数据 - Parse data through my website using cron jobs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM