- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,它需要用户输入来连接到站点,将其 html 下载为文本,并每天两次从表中检索数据。我知道代码不会是适合任何页面的一种尺寸(一旦我开始工作,我可能会将网址“硬连线”到代码中)。我目前的问题是我的 jsoup 解析器无法正确读取表格数据。我不确定我的元素选择器是否太通用?该表看起来像是标准 table/tr/td 格式,但我的 rows 数组填充的大小为 0。如果有人可以帮助我调试我的解析器,并可能提供一些关于在哪里查找的建议,使其每天两次静默抓取数据,我真的很感激!没有运行时/编译错误,只需要纠正输出。
来源站点:https://www.cnbc.com/us-markets/表的源代码(片段):
<table class="BasicTable-table"><thead class="BasicTable-tableHeading BasicTable-tableHeadingSortable"><tr><th class="BasicTable-textData"><span>SYMBOL <span class="icon-sort undefined"></span></span></th><th class="BasicTable-numData"><span>PRICE <span class="icon-sort undefined"></span></span></th><th class="BasicTable-numData">
我的代码:
public class StockScraper {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Enter the complete url (including http://) of the site you would like to parse:");
String html = input.nextLine();
try {
Document doc = Jsoup.connect(html).get();
System.out.printf("Title: %s", doc.title());
//Try to print site content
System.out.println("");
System.out.println("Writing html contents to 'html.txt'...");
//Save html contents to text file
PrintWriter outputfile = new PrintWriter("html.txt");
outputfile.print(doc.outerHtml());
outputfile.close();
//Select stock data you want to retrieve
System.out.println("Enter the name of the stock you want to check");
String name = input.nextLine();
//Pull data from CNBC Markets
Element table = doc.select("table").get(0);
Elements rows = table.select("tr");
System.out.println(rows.size());
for(int i = 1; i < rows.size(); i++) {
Element rowx = rows.get(i);
Elements col = rows.select("td");
if(col.get(0).equals(name)) {
System.out.println("I worked!");
System.out.println(col.get(1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
最佳答案
这里的问题是,该网站是一个动态页面,在浏览器最初下载该页面后才加载内容。 Jsoup 不足以抓取这样的页面。您有几个选择:
1) 使用模拟浏览器并进行所有必要的 API 调用的工具。 Selenium WebDriver 或 HTMLUnit 是两个选项。
2) 找出您在此站点上感兴趣的 api 调用,然后直接调用这些 api 即可获取可以解析的 JSON 文档。您可以通过在浏览器中打开开发人员工具,然后查看“网络”选项卡来查看 api 详细信息。对于此网站,示例如下,其中包括 DJI 的股票报价:
https://quote.cnbc.com/quote-html-webservice/quote.htm?noform=1&partnerId=2&fund=1&exthrs=0&output=json&symbolType=issue&symbols=599362|579435|593933|49020635|49031016|5093160|617254|601065&requestMethod=extended
Returns:
ExtendedQuoteResult: {
xmlns: "http://quote.cnbc.com/services/MultiQuote/2006",
ExtendedQuote: [{
QuickQuote: {
symbol: ".DJI",
code: "0",
curmktstatus: "REG_MKT",
FundamentalData: {
yrlodate: "2020-03-23",
yrloprice: "18213.65",
yrhidate: "2020-02-12",
yrhiprice: "29568.57"
},
mappedSymbol: {
xsi:nil: "true"
},
source: "Exchange",
cnbcId: "599362",
prev_prev_closing: "21413.44",
high: "22783.45",
low: "21693.63",
provider: "CNBC Quote Cache",
streamable: "0",
last_time: "2020-04-06T17:16:28.000-0400",
countryCode: "US",
previous_day_closing: "21052.53",
altName: "Dow Industrials",
reg_last_time: "2020-04-06T17:16:28.000-0400",
last_time_msec: "1586207788000",
altSymbol: ".DJI",
change_pct: "7.73",
providerSymbol: ".DJI",
assetSubType: "Index",
comments: "RIC",
last: "22679.99",
issue_id: "599362",
cacheServed: "false",
responseTime: "Mon Apr 06 19:12:09 EDT 2020",
change: "1627.46",
timeZone: "EDT",
onAirName: "Dow Industrials",
symbolType: "issue",
assetType: "INDEX",
volume: "614200990",
fullVolume: "614200990",
realTime: "true",
name: "Dow Jones Industrial Average",
quoteDesc: { },
exchange: "Dow Jones Global Indexes",
shortName: "DJIA",
cachedTime: "Mon Apr 06 19:12:09 EDT 2020",
currencyCode: "USD",
open: "21693.63"
}
}
...
关于java - 如何解析 CNBC 市场页面的表格数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61070037/
我正在尝试使用 CDE 对我的仪表板进行一些更改。但是当我以管理员身份访问 Pentaho plateform 5.0 时,我无法在所有菜单中找到 Marketplace谢谢你帮助我! 最佳答案 Ma
我将如何从小部件启动设置菜单/市场。我尝试使用代码并使用设置/市场包名称,但它们都不会打开。这是我正在使用的代码: String packageName = "com.package"; String
我有一个应用程序在没有蓝牙的情况下无法在特定设备上看到。问题是在我的 list 文件中我有蓝牙权限: 我以编程方式排除了该设备的蓝牙选项,但我无法在该设备上的市场上找到我的应用程序。 所以,3 个
Microsoft 文档 How to Send Email Using SendGrid with Azure说一旦设置了帐户,您就可以通过单击 key 图标来查看 SendGrid 凭据。这似乎已
我想以编程方式为我的 child 应用程序启用/禁用 Android 市场(带有复选框)。这可能吗?如何? 最佳答案 一般来说,没有。您不能从您的应用程序影响其他应用程序的行为。有一些技巧可以让您监控
我想制作一个按自定义行数加载数据的列表框。更像是市场上的列表框。有什么想法吗? 编辑:我指的是列表框的工作方式。它一次按 x 行加载市场数据。我希望它按 x 数加载数据行,这样应用程序就不必一次加载所
是否有机会根据在应用程序购买期间选择的市场获得固定的本地化列表。比方说,如果用户从德国市场下载它,则只能使用德语本地化,如果是其他市场,则将使用英语本地化。我需要在不检查 GPS 位置的情况下进行管理
我正在尝试创建一个模型,在该模型中,制造商可以发布需要运输的 cargo ,运输商可以发布他的卡车从 A 点到 B 点的信息。 ) 匹配,然后他们都被通知匹配。 我曾尝试研究自动匹配,但最接近的是解决
我在 Ubuntu 11.10 上安装了 Eclipse Indigo,但市场没有显示在“帮助”下,所以我添加了 http://download.eclipse.org/releases/indigo
我已经在 ubuntu 13.04 64 位上安装了 eclipse kepler 64 位。 当我尝试打开 eclipse 市场时,出现以下错误: Cannot open Eclipse Marke
快速提问,因为我似乎找不到任何东西。当您的应用获得新评论/评级时,有什么方法可以启用某种通知吗?我错过了几个,几乎每天都要进行烦人的检查以确保万无一失! 谢谢! 最佳答案 您可以使用 Andlytic
我想使用 eclipse marketplace 来添加 svn 插件,但是当我尝试这样做时我遇到了这个异常: Cannot open Eclipse Marketplace Cannot insta
我在我的应用程序中使用应用内计费我在将其上传到 goolge play 帐户后对其进行测试我添加了测试产品并在设备上对其进行了测试。请注意,应用内结算的所有先决条件均已完成。但它说“无法连接到市场”任
我开始在 BlueSnap 的市场上销售——我销售订阅,我在 BlueSnap 中为我的推销员创建了供应商帐户,他们为我销售。 我基本上只使用发起人的姓名、电子邮件和国家/地区创建了供应商帐户: {
我是 Balanced 的新手,想知道您是否可以定义托管账户的确切含义,以及该余额与 Marketplace 商户余额之间的区别是什么? 如果我是 Marketplace 所有者个人/组织,哪个代表我
我们想创建自己的android市场(网站、服务器端...),有什么官方文档可以引用吗?有没有开源产品/软件/框架可以帮助创建android市场? 最佳答案 Aptoide可能就是您正在寻找的。它是一个
我正在测试 google 提供的计费示例应用程序: http://developer.android.com/guide/market/billing/index.html 一切顺利,我开始尝试购买一
我已经在 android 市场中添加了这些权限。 然后我将应用程序上传到 Android 电子市场。但是我的在Android 4.0.3的平板市场是看不到的。但在 Android Ho
这行不通 Intent marketLaunch = new Intent(Intent.ACTION_VIEW); marketLaunch.setData(Uri.parse("m
有没有办法确定 Android/iOS 应用程序用户的身份,而不要求他使用 google/email/facebook 进行注册? 我想实现这个:用户从 PlayMarket/AppStore 下载应
我是一名优秀的程序员,十分优秀!