Jump to content

How to get data from URL?


Go to solution Solved by Danp2,

Recommended Posts

Hello friends, I need help to get data from URL . 

The URL is here:

https://www.twse.com.tw/rwd/trading/fund/MI_QFIIS?date=20230505&selectType=24&response=json

 

I want to get the value from "data", is it possible?

How should I modify my code? Can anyone help me...

#include<Array.au3>
#include<Date.au3>
#include<FileConstants.au3>
#include<wd_core_au3>
#include<wd_helper.au3>
#include<Json.au3>
#include<String.au3>
#include<File.au3>
#include<WinHttp.au3>
#include<WinHttpConstants.au3>
#include <Inet.au3>

$URL = "https://www.twse.com.tw/rwd/trading/fund/MI_QFIIS?date=20230505&selectType=24&response=json"
$data = _INetGetSource($URL)
$object = json_decode($data)

Local $i = 0
While 1
    $oData = json_get($object, '[' & $i & '].data')
    ConsoleWrite('$oData== ' & oData & @CRLF)
    $i += 1
WEnd

I got the output: 

$oData==
Link to comment
Share on other sites

  • Solution

Try this --

#include "json.au3"
#include <Inet.au3>

$URL = "https://www.twse.com.tw/rwd/trading/fund/MI_QFIIS?date=20230505&selectType=24&response=json"
$data = _INetGetSource($URL)
$oJSON = json_decode($data)

Local $i = 0

While Not @error
    $oData = Json_Get($oJSON, '.data[' & $i & ']')
    If Not @error Then
        $sData = Json_Encode($oData)
        ConsoleWrite('Data #' & $i & @CRLF)
        Json_Dump($sData)
    EndIf
    $i += 1
WEnd

 

Link to comment
Share on other sites

Here's another way:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <jq\jq.au3>
#include <Array.au3>

Const $JQ_EXE = "C:\Utils\jq\jq-win64.exe"

example()

Func example()
    Local $sJson   = "", _
          $sResult = ""

    Local $aResult[0][12]

    Local $hTimer = 0

    ;Get JSON result from the web
    $sJson = InetRead("https://www.twse.com.tw/rwd/trading/fund/MI_QFIIS?date=20230505&selectType=24&response=json")
    If @error Then Return MsgBox($MB_ICONERROR, "InetRead() Error", "@error = " & @error)
    $sJson = BinaryToString($sJson, $SB_UTF8)

    ;Start timer
    $hTimer = TimerInit()

    ;Initialize jq
    _jqInit($JQ_EXE)
    If @error Then Return MsgBox($MB_ICONERROR, "_jqInit() Error", "@error = " & @error)

    ;Get "data" from JSON.  Return as a tab-separated-value list.
    $sResult = _jqExec($sJson, ".data[]|@tsv")
    If @error Then Return MsgBox($MB_ICONERROR, "_jqExec() Error", "@error = " & @error)

    ;Add data to an array
    _ArrayAdd($aResult, $sResult, 0, @TAB)
    If @error Then Return MsgBox($MB_ICONERROR, "_ArrayAdd() Error", "@error = " & @error)

    ConsoleWrite(StringFormat("JSON processing time: %.3f seconds.",  TimerDiff($hTimer)/1000) & @CRLF)

    ;Display array
    _ArrayDisplay($aResult)
EndFunc

Console output:

JSON processing time: 0.059 seconds.

Resulting array:

image.png.c5a18e42095dd51e577cd4e94bd686e4.png

 

Result in jqPlayground:

image.png.887443bb6ae205bf97f190c5172ae208.png

Edited by TheXman
Link to comment
Share on other sites

Maybe NTP ?

 

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

I updated the JSON  UDF that you gave me and it could work! But when I add  

ConsoleWrite(“$output: ”& _Dump($sData ) & @CRLF)

want to print the data, the output was

$output: 0

I don’t know why it print “0”?

Link to comment
Share on other sites

_Dump() ?

why not 
Json_Dump()

 

?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • Developers
26 minutes ago, Flora said:

Oops…

I missed to paste (my mistake )

The code is again not valid as there's a space after json!
Please post your exact code into a codeblock ( post code <>) here so we can see what you are doing. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...