编辑的
我的CSVRecord类型如下所示:
export class CSVRecord {
public id: any;
public pubversion: any;
public guid: any;
public keywords: any;
public seometriccount: any;
}我正在尝试从我的csv中读取值并在td中显示它,这是我能够从下面的代码中实现的。
app.component.html
<input type="file" #csvReader name="Upload CSV" id="txtFileUpload" (change)="uploadListener($event)" accept=".csv" />
<table class="minimalistBlack" *ngIf="records.length > 0">
<thead>
<tr>
<th>ID </th>
<th>Pub version </th>
<th>GUID </th>
<th>Keywords </th>
<th>SEO Metric count </th>
</tr>
</thead>
<tbody>
<tr *ngFor="let record of records; let i = index;">
<td> <span>{{record.id}}</span> </td>
<td> <span>{{record.pubversion}}</span> </td>
<td> <span>{{record.guid}}</span> </td>
<td> <span>{{ record.keywords }}</span> </td>
<td> <span>{{ record.seometriccount }}</span> </td>
</tr>
</tbody>
</table>app.component.ts
import { Component, ViewChild } from '@angular/core';
import { CSVRecord } from './CSVModel';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Angular7-readCSV';
public records: any[] = [];
@ViewChild('csvReader') csvReader: any;
uploadListener($event: any): void {
let text = [];
let files = $event.srcElement.files;
if (this.isValidCSVFile(files[0])) {
let input = $event.target;
let reader = new FileReader();
reader.readAsText(input.files[0]);
reader.onload = () => {
let csvData = reader.result;
console.log(csvData);
let csvRecordsArray = (<string>csvData).split(/\r\n|\n/);
let headersRow = this.getHeaderArray(csvRecordsArray);
this.records = this.getDataRecordsArrayFromCSVFile(csvRecordsArray, headersRow.length);
};
reader.onerror = function () {
console.log('error is occured while reading file!');
};
} else {
alert("Please import valid .csv file.");
this.fileReset();
}
}
getDataRecordsArrayFromCSVFile(csvRecordsArray: any, headerLength: any) {
let csvArr = [];
for (let i = 1; i < csvRecordsArray.length; i++) {
let curruntRecord = (<string>csvRecordsArray[i]).split(',');
if (curruntRecord.length == headerLength) {
let csvRecord: CSVRecord = new CSVRecord();
csvRecord.id = curruntRecord[0].trim();
csvRecord.pubversion = curruntRecord[1].trim();
csvRecord.guid = curruntRecord[2].trim();
csvRecord.keywords = curruntRecord[3].trim();
csvRecord.seometriccount = curruntRecord[4];
csvArr.push(csvRecord);
}
}
return csvArr;
}
isValidCSVFile(file: any) {
return file.name.endsWith(".csv");
}
getHeaderArray(csvRecordsArr: any) {
let headers = (<string>csvRecordsArr[0]).split(',');
let headerArray = [];
for (let j = 0; j < headers.length; j++) {
headerArray.push(headers[j]);
}
return headerArray;
}
fileReset() {
this.csvReader.nativeElement.value = "";
this.records = [];
}
}输出:

现在,我需要在输出的最后一列中显示google搜索结果计数,使用关键字(来自上一篇专栏)。
如果我的Keywords列包含诸如- Amazon,Firestick,bla之类的关键字。然后,我的脚本应该搜索谷歌这些关键字,并显示从第1页的结果计数。
在第1页中说,它有5个结果,它应该显示5个。
1)我如何做到这一点?-这能用角度来完成吗?
2)请用代码解释,因为我对角7很陌生。
发布于 2019-09-20 10:02:31
首先,你想要达到的目标,根本就不是用户友好的。记住,当您每次在模型或UI中更改内容时,角将尝试绑定您的单元格(我不确定,但有时即使当您滚动页面时,它也会尝试重新绘制UI,因此您需要缓存结果,其他明智的方法是,您将不断向google提出请求,这可能会导致您的客户IP地址得到captcha而没有结果返回)。
因此,为了尽量减少这种影响:
在html中:
<td> <span>{{ GetResultFromGoogle(record) }}</span> </td>在Ts中:
public GetResultFromGoogle(record:CSVRecord ):number {
if(!record.seometriccount)
{
// search google for result and get what you want, answering to this part, is for another quesiton
this.httpClient.get(googleUrl, neededParamsForSearch).subscribe( data=>{
//extract result by Xpath and set to your record
return record.seometriccount = resultCount;
});
}
else
return record.seometriccount;
}https://stackoverflow.com/questions/57955208
复制相似问题