从网页中提取值

时间:2017-12-31 09:48:58

标签: excel-vba web-scraping vba excel

美好的一天 - 我确信无数次都得到了解决,但我很沮丧,我尝试了所有可行的方法,仍然无法让它发挥作用。

我正在尝试从具有以下代码的网站中获取值:
enter image description here

我只需要提取169.23

这是我的代码无效:

Sub ImportCurrentPrice2()

    Dim appIE As Object
    Set appIE = CreateObject("internetexplorer.application")

    Visible = True

    With appIE
    .navigate "https://finance.google.com/finance?q=AAPL"
    .Visible = False
    End With

    Do While appIE.Busy
    DoEvents
    Loop

    Set allRowOfData = appIE.document.getElementsByClassName("hilite")

    Dim myValue As String: myValue = allRowOfData.Cells(4).innerHTML

    appIE.Quit
    Set appIE = Nothing
    Range("A1").Value = myValue

End Sub

这会返回错误。

有关上述代码需要修改的建议吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

试一试并找到工作表中的值。

 try{
            Class.forName("com.mysql.jdbc.Driver");
            Core.con = DriverManager.getConnection(Core.db,Core.dbUsername,Core.dbPassword);
            java.sql.Statement st = Core.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = st.executeQuery(Core.Show);

            while(rs.next()){
                table.addItem(new Object[]{rs.getInt(1), rs.getString(2), rs.getDate(3)},rs.getInt(1));

            }
        }catch(Exception e){
            e.printStackTrace();
        }

        table.addItemClickListener(new ItemClickEvent.ItemClickListener(){

            @Override
            public void itemClick(ItemClickEvent event) {
                Name = new TextField("Name");
                Name.setRequired(true);
                TextField Name1 = new TextField("Name");
                Name1.setVisible(false);
                Date = new DateField("Date");
                Date.setDateFormat("yyyy-MM-dd");
                Date.setRequired(true);
                DateField Date1 = new DateField("Date");
                Date1.setDateFormat("yyyy-MM-dd");
                Date1.setVisible(false);
                Id = new TextField();
                Id.setVisible(false);


                Name.setValue((String) event.getItem().getItemProperty("Name").getValue());
                Name1.setValue((String) event.getItem().getItemProperty("Name").getValue());
                Date.setValue((Date)event.getItem().getItemProperty("Date").getValue());
                Date1.setValue((Date)event.getItem().getItemProperty("Date").getValue());

                Save = new Button("Save");
                Save.setStyleName("small");
                Save.setStyleName("primary");
                Save.setSizeUndefined();

                Save.addClickListener(new Button.ClickListener(){

                    @Override
                    public void buttonClick(ClickEvent event) {
                        try{
                            Class.forName("com.mysql.jdbc.Driver");
                            Core.con = DriverManager.getConnection(Core.db,Core.dbUsername,Core.dbPassword);
                            Core.stmt = Core.con.prepareStatement(Core.Replace);
                            Core.statement = Core.con.prepareStatement(Core.ReplaceDate);
                            Core.stmt.setString(1,Name.getValue().toString());
                            Core.stmt.setString(2,Name1.getValue().toString());
                            Core.stmt.setDate(3,new java.sql.Date(Date1.getValue().getTime()));
                            Core.statement.setDate(1,new java.sql.Date(Date.getValue().getTime()));
                            Core.statement.setString(2,Name1.getValue().toString());
                            Core.statement.setDate(3,new java.sql.Date(Date1.getValue().getTime()));
                            Core.stmt.executeUpdate();
                            Core.statement.executeUpdate();
                            Notification.show("done");
                            Name.setVisible(false);
                            Date.setVisible(false);
                            Save.setVisible(false);
                            Delete.setVisible(false);
                        }catch(Exception e){
                            e.printStackTrace();
                        }

                    }

                });

                Delete = new Button("Delete");
                Delete.setStyleName("small");
                Delete.setStyleName("danger");
                Delete.setSizeUndefined();

                Delete.addClickListener(new Button.ClickListener(){
                    @Override
                    public void buttonClick (ClickEvent ev){
                        try{
                            Class.forName("com.mysql.jdbc.Driver");
                            Core.con = DriverManager.getConnection(Core.db,Core.dbUsername,Core.dbPassword);
                            Core.stmt = Core.con.prepareStatement(Core.Delete);
                            Core.stmt.setString(1,Name1.getValue().toString());
                            Core.stmt.setDate(2,new java.sql.Date(Date1.getValue().getTime()));
                            Core.stmt.executeUpdate();
                            Notification.show("done");
                            Name.setVisible(false);
                            Date.setVisible(false);
                            Save.setVisible(false);
                            Delete.setVisible(false);
                        }catch(Exception ex){
                            Notification.show(ex.getMessage());
                        }
                    }
                });


                show.addComponent(Name);
                show.addComponent(Date);
                Edit.addComponent(Save);
                Edit.addComponent(Delete);
                show.setSpacing(true);
                show.addComponent(Edit);



            }

        });

OR:

Sub ImportCurrentPrice2()
    Dim appIE As New InternetExplorer, html As HTMLDocument
    Dim item_data As Object

    With appIE
        .Visible = True
        .navigate "https://finance.google.com/finance?q=AAPL"
        Do Until .readyState = 4: DoEvents: Loop
        Set html = .document
    End With

    Set item_data = html.queryselector(".pr span")
    [A1] = item_data.innerText

    appIE.Quit
End Sub