如何在基于Java的Selenium API中,每天拉取股票指数两次?

huangapple 未分类评论42阅读模式
英文:

How to pull stock indexes in a Java-based Selenium API twice a day?

问题

以下是翻译好的部分:

我有一个能够自动连接到CNBC市场页面(https://www.cnbc.com/us-markets/)的Selenium代码正在运行。我之前尝试过使用Jsoup使这个程序工作,但事实证明该网站实时更新,获取这些数据的最佳方式要么是获取一个JSON文件,要么是使用Selenium(据我所知)。鉴于我已经编写了Selenium来远程连接到该网站,我想知道如何查询页面上的股票。我宁愿避免学习大量的JavaScript来实现这一点,但如果没有其他实际方法,我也可以考虑。我想知道JQuery是否是最佳工具?

以下是我的代码:

// 由于Selenium兼容性问题,使用Java 1.7构建

// 导入包
import java.io.File;
import java.util.Scanner;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Tester {

    public static void main(String[] args) {
        // 指定Selenium使用Chrome驱动文件
        File file = new File("C:\\Users\\zrr81\\eclipse-workspace\\WebScraper\\Selenium\\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

        // 初始化Chrome驱动并选择站点
        WebDriver driver = new ChromeDriver();
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要连接的网址:");
        String url = input.nextLine();

        // 尝试连接,捕获拼写错误/错误的异常
        try {
            driver.get(url);

        } catch (InvalidArgumentException e) {
            System.out.println("他挂了,吉姆! :/");
        }
        input.close();
    }
}

编辑:为了澄清,我需要基于用户输入的股票代码查询股票股价(例如,如果用户输入了DJIA,我需要获取道琼斯的股票价格)。

英文:

I have a Selenium code working that auto connects to the CNBC markets page (https://www.cnbc.com/us-markets/). I had previously looked into getting this program working with Jsoup, but it turns out that the site is updated in realtime and the best way to pull this data is either to pull a JSON file or to use Selenium (as I understand it). Given that I've programmed Selenium to remote connect to the site, I'd like to know how to go about querying a stock on the page. I'd prefer to avoid learning a bunch of Javascript to make this happen, but I'm open to it if there's no other practical approach. I wonder if JQuery is the best tool?

Here's my code I have:

//Built in Java 1.7 due to Selenium compatabilities

//Import packages
import java.io.File;
import java.util.Scanner;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Tester {

public static void main(String[] args) {
	//Point Selenium to Chrome Drive file
     File file = new File("C:\\Users\\zrr81\\eclipse-workspace\\WebScraper\\Selenium\\chromedriver.exe");
     System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
     
     //Initialize Chrome driver and choose site
     WebDriver driver = new ChromeDriver();
     Scanner input = new Scanner(System.in);
     System.out.println("Enter the url you would like to connect to: ");
     String url = input.nextLine();
     
     //Try to connect, include catch for typos/errors
     try {
         driver.get(url);
         
     }
     catch(InvalidArgumentException e) {
    	 System.out.println("He's dead Jim! :/");
     }
     input.close();
}
}

EDIT: To clarify, I need to query a stock share price based on user input for the ticker (e.g. if the user inputs DJIA I need to pull the stock price for Dow Jones)

huangapple
  • 本文由 发表于 2020年4月8日 11:04:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61092687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定