首页 > 解决方案 > 字符串比较不适用于从网络抓取收到的文本

问题描述

我正在将从网络抓取收到的文本与我的代码中的硬编码文本进行比较。两个文本是相同的。没有大写小错误。它们是相同的,但比较仍然失败。我正在分享我的代码的一部分。问题出在第 47 到 56 行之间。在这些行之间,if else 块中的字符串比较失败。为这些块提供的值是完美的值,应该理想地满足条件。49 处的 if 条件由于某种原因得到满足,而另一个 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 条件应该得到满足,元素 e 应该放在 mapItemNameToElement 中。

标签: dartflutterdart-html

解决方案


您的 html 字符串具有 html 实体

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

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


推荐阅读