我正在比较从网络抓取中获得的文本与代码中的硬编码文本。这两个文本是相同的。无大写小错误。它们是相同的,但比较仍然失败。我正在共享我的代码的一部分。 问题出在第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®ion=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中。
答案 0 :(得分:2)
您的html字符串具有html实体
Dividends INR does not equal Dividends <span>INR
在进行比较之前,使用https://pub.dartlang.org/packages/html_unescape来解码项目标题