字符串比较不适用于从网络抓取收到的文本

问题描述 投票:0回答:1

我将从网页抓取中收到的文本与我的代码中的硬编码文本进行比较。这两个文本完全相同。没有资本 - 小错误。它们是相同的,但仍然比较失败。我正在分享我的代码的一部分。问题出在第47到56行之间。在这些行之间,if else块中的字符串比较失败。为这些块提供的值是非常精细的值,理想情况下应满足条件。如果条件不满意,49的if条件由于某种原因而满足。这种行为太奇怪了。在Java中转换时相同的代码运行并且工作正常,没有毛刺执行所有if条件。请看看并帮忙。谢谢。

我也用开关盒试过这个,但也失败了。

 import 'package:http/http.dart';
 import 'package:html/parser.dart';
 import 'package:html/dom.dart';
 import 'dart:convert';
 class Worker{

 static final String OperatingCashFlowINRMil = 'Operating Cash Flow INR Mil';
 static final String CapSpendingINRMil = 'Cap Spending INR Mil';
 static final String FreeCashFlowINRMil = 'Free Cash Flow INR Mil';
 static final String DividendsINR = 'Dividends INR';
 static final String DividendPayoutRatio = 'Payout Ratio % *';
 static Map<String,String> _RequestHeaders = Map<String,String>();

 static void fetchData() async
 {
 String MSUrlToGetFinancialData =
    "https://financials.morningstar.com/finan/financials/getFinancePart.html?&callback=jsonp1553353302056&t=0P0000AX98&region=ind&culture=en-US&version=SAL&cur=&order=desc&_=1553353302079";
Client client = Client();

Response response2 = await client.get(MSUrlToGetFinancialData,
    headers: getRequestHeaders());

var FinDataResponse = response2.body;

FinDataResponse = FinDataResponse.replaceAll("jsonp1553353302056(", "");
FinDataResponse =
    FinDataResponse.substring(0, FinDataResponse.length - 1);

JsonDecoder jsonDecoder = JsonDecoder();
var FinDataJson = jsonDecoder.convert(FinDataResponse);
String FinDataString = FinDataJson["componentData"];
Element FinDataDoc = parse(FinDataString).body;
Element DataTable = FinDataDoc.querySelector("table");
List<Element> lstYears = DataTable.querySelector("thead")
    .querySelector("tr")
    .querySelectorAll("th");
List<Element> lstRows =
DataTable.querySelector("tbody").querySelectorAll("tr");

Map<String, Element> mapItemNameToElement = Map<String, Element>();

///////////////////////////////////////////////////////////////////////////0
for (Element e in lstRows) {
  String ItemHeading = e.children[0].text.trim().toString();
  print(ItemHeading);//The identical values which can satisfy the following conditions can be seen printed here.

  if (ItemHeading == DividendsINR) {//This condition does not get satisfied even when the ItemHeading value is identical.
    mapItemNameToElement.putIfAbsent(DividendsINR, () => e);
  } else if (ItemHeading == DividendPayoutRatio) {//This condition gets satisfied.
    mapItemNameToElement.putIfAbsent(DividendPayoutRatio, () => e);
  } else if (ItemHeading == OperatingCashFlowINRMil) {//This condition does not get satisfied even when the ItemHeading value is identical.
    mapItemNameToElement.putIfAbsent(OperatingCashFlowINRMil, () => e);
  } else if (ItemHeading == CapSpendingINRMil) {//This condition does not get satisfied even when the ItemHeading value is identical.
    mapItemNameToElement.putIfAbsent(CapSpendingINRMil, () => e);
  } else if (ItemHeading == FreeCashFlowINRMil) {//This condition does not get satisfied even when the ItemHeading value is identical.
    mapItemNameToElement.putIfAbsent(FreeCashFlowINRMil, () => e);
  }
}
}


static Map<String,String> getRequestHeaders()
{
if(_RequestHeaders.length == 0)
{
  _RequestHeaders.putIfAbsent("Accept-Encoding", () => "gzip, deflate, br");
  _RequestHeaders.putIfAbsent("referer", () => "https://www.morningstar.com/");
  _RequestHeaders.putIfAbsent("user-agent", () => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
  _RequestHeaders.putIfAbsent("authority", () => "www.morningstar.com");
}
return _RequestHeaders;
}
}

我的pubspec.yaml:

name: dev1_stock_meter
description: A new Flutter application.

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.2.5+1
  firebase_auth: ^0.7.0
  cloud_firestore:
  fluttertoast: ^3.0.4
  autocomplete_textfield: ^1.6.4
  html: ^0.13.3+3
  http: ^0.12.0
  date_format: ^1.0.6
  intl:
  csv: ^4.0.3
  xml:
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
  sdk: flutter

flutter:
  uses-material-design: true

assets:
  - images/logo.jpg

fonts:
  - family: GoogleSans
    fonts:
      - asset: fonts/GoogleSans-Regular.ttf
        weight: 300
      - asset: fonts/GoogleSans-Bold.ttf
        weight: 400

预期结果:if条件应该得到满足,Element e应该放在mapItemNameToElement中。

dart flutter dart-html
1个回答
2
投票

你的html字符串有html实体

Dividends INR does not equal Dividends&nbsp;<span>INR

在进行比较之前使用https://pub.dartlang.org/packages/html_unescape解码项目头

© www.soinside.com 2019 - 2024. All rights reserved.