Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Web-scraping/00-META.yaml
Normal file
5
Task/Web-scraping/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Input_Output
|
||||
from: http://rosettacode.org/wiki/Web_scraping
|
||||
note: Networking and Web Interaction
|
||||
28
Task/Web-scraping/00-TASK.txt
Normal file
28
Task/Web-scraping/00-TASK.txt
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
;Task:
|
||||
Create a program that downloads the time from this URL: [http://tycho.usno.navy.mil/cgi-bin/timer.pl http://tycho.usno.navy.mil/cgi-bin/timer.pl] and then prints the current UTC time by extracting just the UTC time from the web page's [[HTML]]. Alternatively, if the above url is not working, grab the first date/time off this page's talk page.
|
||||
|
||||
<!-- As of March 2014, the page is available
|
||||
{{task|Networking and Web Interaction}}
|
||||
|
||||
The page http://tycho.usno.navy.mil/cgi-bin/timer.pl is no longer available since July 2011.
|
||||
The relevant part of that page source looked like this:
|
||||
<pre>
|
||||
...
|
||||
|
||||
<TITLE>What time is it?</TITLE>
|
||||
<H2> US Naval Observatory Master Clock Time</H2> <H3><PRE>
|
||||
<BR>Jul. 27, 22:57:22 UTC Universal Time
|
||||
<BR>Jul. 27, 06:57:22 PM EDT Eastern Time
|
||||
<BR>Jul. 27, 05:57:22 PM CDT Central Time
|
||||
<BR>Jul. 27, 04:57:22 PM MDT Mountain Time
|
||||
<BR>Jul. 27, 03:57:22 PM PDT Pacific Time
|
||||
<BR>Jul. 27, 02:57:22 PM AKDT Alaska Time
|
||||
<BR>Jul. 27, 12:57:22 PM HAST Hawaii-Aleutian Time
|
||||
|
||||
...
|
||||
</pre>
|
||||
End of comment -->
|
||||
|
||||
If possible, only use libraries that come at no ''extra'' monetary cost with the programming language and that are widely available and popular such as [http://www.cpan.org/ CPAN] for Perl or [[Boost]] for C++.
|
||||
<br><br>
|
||||
|
||||
14
Task/Web-scraping/8th/web-scraping.8th
Normal file
14
Task/Web-scraping/8th/web-scraping.8th
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
\ Web-scrape sample: get UTC time from the US Naval Observatory:
|
||||
: read-url \ -- s
|
||||
"http://tycho.usno.navy.mil/cgi-bin/timer.pl" net:get
|
||||
not if "Could not connect" throw then
|
||||
>s ;
|
||||
|
||||
: get-time
|
||||
read-url
|
||||
/<BR>.*?(\d{2}:\d{2}:\d{2})\sUTC/
|
||||
tuck r:match if
|
||||
1 r:@ . cr
|
||||
then ;
|
||||
|
||||
get-time bye
|
||||
42
Task/Web-scraping/ALGOL-68/web-scraping.alg
Normal file
42
Task/Web-scraping/ALGOL-68/web-scraping.alg
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
STRING
|
||||
domain="tycho.usno.navy.mil",
|
||||
page="cgi-bin/timer.pl";
|
||||
|
||||
STRING # search for the needle in the haystack #
|
||||
needle = "UTC",
|
||||
hay stack = "http://"+domain+"/"+page,
|
||||
|
||||
re success="^HTTP/[0-9.]* 200",
|
||||
re result description="^HTTP/[0-9.]* [0-9]+ [a-zA-Z ]*",
|
||||
re doctype ="\s\s<![Dd][Oo][Cc][Tt][Yy][Pp][Ee] [^>]+>\s+";
|
||||
|
||||
PROC raise error = (STRING msg)VOID: ( put(stand error, (msg, new line)); stop);
|
||||
|
||||
PROC is html page = (REF STRING page) BOOL: (
|
||||
BOOL out=grep in string(re success, page, NIL, NIL) = 0;
|
||||
IF INT start, end;
|
||||
grep in string(re result description, page, start, end) = 0
|
||||
THEN
|
||||
page:=page[end+1:];
|
||||
IF grep in string(re doctype, page, start, end) = 0
|
||||
THEN page:=page[start+2:]
|
||||
ELSE raise error("unknown format retrieving page")
|
||||
FI
|
||||
ELSE raise error("unknown error retrieving page")
|
||||
FI;
|
||||
out
|
||||
);
|
||||
|
||||
STRING reply;
|
||||
INT rc = http content (reply, domain, haystack, 0);
|
||||
IF rc = 0 AND is html page (reply)
|
||||
THEN
|
||||
STRING line; FILE freply; associate(freply, reply);
|
||||
on logical file end(freply, (REF FILE freply)BOOL: (done; SKIP));
|
||||
DO
|
||||
get(freply,(line, new line));
|
||||
IF string in string(needle, NIL, line) THEN print((line, new line)) FI
|
||||
OD;
|
||||
done: SKIP
|
||||
ELSE raise error (strerror (rc))
|
||||
FI
|
||||
21
Task/Web-scraping/AWK/web-scraping.awk
Normal file
21
Task/Web-scraping/AWK/web-scraping.awk
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#! /usr/bin/awk -f
|
||||
|
||||
BEGIN {
|
||||
purl = "/inet/tcp/0/tycho.usno.navy.mil/80"
|
||||
ORS = RS = "\r\n\r\n"
|
||||
print "GET /cgi-bin/timer.pl HTTP/1.0" |& purl
|
||||
purl |& getline header
|
||||
while ( (purl |& getline ) > 0 )
|
||||
{
|
||||
split($0, a, "\n")
|
||||
for(i=1; i <= length(a); i++)
|
||||
{
|
||||
if ( a[i] ~ /UTC/ )
|
||||
{
|
||||
sub(/^<BR>/, "", a[i])
|
||||
printf "%s\n", a[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
close(purl)
|
||||
}
|
||||
34
Task/Web-scraping/Ada/web-scraping.ada
Normal file
34
Task/Web-scraping/Ada/web-scraping.ada
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
with AWS.Client, AWS.Response, AWS.Resources, AWS.Messages;
|
||||
with Ada.Text_IO, Ada.Strings.Fixed;
|
||||
use Ada, AWS, AWS.Resources, AWS.Messages;
|
||||
|
||||
procedure Get_UTC_Time is
|
||||
|
||||
Page : Response.Data;
|
||||
File : Resources.File_Type;
|
||||
Buffer : String (1 .. 1024);
|
||||
Position, Last : Natural := 0;
|
||||
S : Messages.Status_Code;
|
||||
begin
|
||||
Page := Client.Get ("http://tycho.usno.navy.mil/cgi-bin/timer.pl");
|
||||
S := Response.Status_Code (Page);
|
||||
if S not in Success then
|
||||
Text_IO.Put_Line
|
||||
("Unable to retrieve data => Status Code :" & Image (S) &
|
||||
" Reason :" & Reason_Phrase (S));
|
||||
return;
|
||||
end if;
|
||||
|
||||
Response.Message_Body (Page, File);
|
||||
while not End_Of_File (File) loop
|
||||
Resources.Get_Line (File, Buffer, Last);
|
||||
Position :=
|
||||
Strings.Fixed.Index
|
||||
(Source => Buffer (Buffer'First .. Last),
|
||||
Pattern => "UTC");
|
||||
if Position > 0 then
|
||||
Text_IO.Put_Line (Buffer (5 .. Position + 2));
|
||||
return;
|
||||
end if;
|
||||
end loop;
|
||||
end Get_UTC_Time;
|
||||
9
Task/Web-scraping/App-Inventor/web-scraping.app
Normal file
9
Task/Web-scraping/App-Inventor/web-scraping.app
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
when ScrapeButton.Click do
|
||||
set ScrapeWeb.Url to SourceTextBox.Text
|
||||
call ScrapeWeb.Get
|
||||
|
||||
when ScrapeWeb.GotText url,responseCode,responseType,responseContent do
|
||||
initialize local Left to split at first text (text: get responseContent, at: PreTextBox.Text)
|
||||
initialize local Right to "" in
|
||||
set Right to select list item (list: get Left, index: 2)
|
||||
set ResultLabel.Text to select list item (list: split at first (text:get Right, at: PostTextBox.Text), index: 1)
|
||||
33
Task/Web-scraping/AppleScript/web-scraping-1.applescript
Normal file
33
Task/Web-scraping/AppleScript/web-scraping-1.applescript
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
|
||||
use framework "Foundation"
|
||||
|
||||
on firstDateonWebPage(URLText)
|
||||
set |⌘| to current application
|
||||
set pageURL to |⌘|'s class "NSURL"'s URLWithString:(URLText)
|
||||
-- Fetch the page HTML as data.
|
||||
-- The Xcode documentation advises against using dataWithContentsOfURL: over a network,
|
||||
-- but I'm guessing this applies to downloading large files rather than a Web page's HTML.
|
||||
-- If in doubt, the HTML can be fetched as text instead and converted to data in house.
|
||||
set HTMLData to |⌘|'s class "NSData"'s dataWithContentsOfURL:(pageURL)
|
||||
-- Or:
|
||||
(*
|
||||
set {HTMLText, encoding} to |⌘|'s class "NSString"'s stringWithContentsOfURL:(pageURL) ¬
|
||||
usedEncoding:(reference) |error|:(missing value)
|
||||
set HTMLData to HTMLText's dataUsingEncoding:(encoding)
|
||||
*)
|
||||
|
||||
-- Extract the page's visible text from the HTML.
|
||||
set straightText to (|⌘|'s class "NSAttributedString"'s alloc()'s initWithHTML:(HTMLData) ¬
|
||||
documentAttributes:(missing value))'s |string|()
|
||||
|
||||
-- Use an NSDataDetector to locate the first date in the text. (It's assumed here there'll be one.)
|
||||
set dateDetector to |⌘|'s class "NSDataDetector"'s dataDetectorWithTypes:(|⌘|'s NSTextCheckingTypeDate) ¬
|
||||
|error|:(missing value)
|
||||
set matchRange to dateDetector's rangeOfFirstMatchInString:(straightText) options:(0) ¬
|
||||
range:({0, straightText's |length|()})
|
||||
|
||||
-- Return the date text found.
|
||||
return (straightText's substringWithRange:(matchRange)) as text
|
||||
end firstDateonWebPage
|
||||
|
||||
firstDateonWebPage("https://www.rosettacode.org/wiki/Talk:Web_scraping")
|
||||
1
Task/Web-scraping/AppleScript/web-scraping-2.applescript
Normal file
1
Task/Web-scraping/AppleScript/web-scraping-2.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
"20:59, 30 May 2020"
|
||||
4
Task/Web-scraping/AutoHotkey/web-scraping.ahk
Normal file
4
Task/Web-scraping/AutoHotkey/web-scraping.ahk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
UrlDownloadToFile, http://tycho.usno.navy.mil/cgi-bin/timer.pl, time.html
|
||||
FileRead, timefile, time.html
|
||||
pos := InStr(timefile, "UTC")
|
||||
msgbox % time := SubStr(timefile, pos - 9, 8)
|
||||
18
Task/Web-scraping/BBC-BASIC/web-scraping.basic
Normal file
18
Task/Web-scraping/BBC-BASIC/web-scraping.basic
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
SYS "LoadLibrary", "URLMON.DLL" TO urlmon%
|
||||
SYS "GetProcAddress", urlmon%, "URLDownloadToFileA" TO UDTF%
|
||||
SYS "LoadLibrary", "WININET.DLL" TO wininet%
|
||||
SYS "GetProcAddress", wininet%, "DeleteUrlCacheEntryA" TO DUCE%
|
||||
|
||||
url$ = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
file$ = @tmp$+"navytime.txt"
|
||||
|
||||
SYS DUCE%, url$
|
||||
SYS UDTF%, 0, url$, file$, 0, 0 TO result%
|
||||
IF result% ERROR 100, "Download failed"
|
||||
|
||||
file% = OPENIN(file$)
|
||||
REPEAT
|
||||
text$ = GET$#file%
|
||||
IF INSTR(text$, "UTC") PRINT MID$(text$, 5)
|
||||
UNTIL EOF#file%
|
||||
CLOSE #file%
|
||||
23
Task/Web-scraping/C++/web-scraping.cpp
Normal file
23
Task/Web-scraping/C++/web-scraping.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
int main()
|
||||
{
|
||||
boost::asio::ip::tcp::iostream s("tycho.usno.navy.mil", "http");
|
||||
if(!s)
|
||||
std::cout << "Could not connect to tycho.usno.navy.mil\n";
|
||||
s << "GET /cgi-bin/timer.pl HTTP/1.0\r\n"
|
||||
<< "Host: tycho.usno.navy.mil\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
<< "Connection: close\r\n\r\n" ;
|
||||
for(std::string line; getline(s, line); )
|
||||
{
|
||||
boost::smatch matches;
|
||||
if(regex_search(line, matches, boost::regex("<BR>(.+\\s+UTC)") ) )
|
||||
{
|
||||
std::cout << matches[1] << '\n';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Task/Web-scraping/C-sharp/web-scraping.cs
Normal file
24
Task/Web-scraping/C-sharp/web-scraping.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
WebClient wc = new WebClient();
|
||||
Stream myStream = wc.OpenRead("http://tycho.usno.navy.mil/cgi-bin/timer.pl");
|
||||
string html = "";
|
||||
using (StreamReader sr = new StreamReader(myStream))
|
||||
{
|
||||
while (sr.Peek() >= 0)
|
||||
{
|
||||
html = sr.ReadLine();
|
||||
if (html.Contains("UTC"))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Console.WriteLine(html.Remove(0, 4));
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
48
Task/Web-scraping/C/web-scraping.c
Normal file
48
Task/Web-scraping/C/web-scraping.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <curl/curl.h>
|
||||
#include <sys/types.h>
|
||||
#include <regex.h>
|
||||
|
||||
#define BUFSIZE 16384
|
||||
|
||||
size_t lr = 0;
|
||||
|
||||
size_t filterit(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||
{
|
||||
if ( (lr + size*nmemb) > BUFSIZE ) return BUFSIZE;
|
||||
memcpy(stream+lr, ptr, size*nmemb);
|
||||
lr += size*nmemb;
|
||||
return size*nmemb;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
CURL *curlHandle;
|
||||
char buffer[BUFSIZE];
|
||||
regmatch_t amatch;
|
||||
regex_t cregex;
|
||||
|
||||
curlHandle = curl_easy_init();
|
||||
curl_easy_setopt(curlHandle, CURLOPT_URL, "http://tycho.usno.navy.mil/cgi-bin/timer.pl");
|
||||
curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, filterit);
|
||||
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, buffer);
|
||||
int success = curl_easy_perform(curlHandle);
|
||||
curl_easy_cleanup(curlHandle);
|
||||
|
||||
buffer[lr] = 0;
|
||||
|
||||
regcomp(&cregex, " UTC", REG_NEWLINE);
|
||||
regexec(&cregex, buffer, 1, &amatch, 0);
|
||||
int bi = amatch.rm_so;
|
||||
while ( bi-- > 0 )
|
||||
if ( memcmp(&buffer[bi], "<BR>", 4) == 0 ) break;
|
||||
|
||||
buffer[amatch.rm_eo] = 0;
|
||||
|
||||
printf("%s\n", &buffer[bi+4]);
|
||||
|
||||
regfree(&cregex);
|
||||
return 0;
|
||||
}
|
||||
31
Task/Web-scraping/Ceylon/web-scraping.ceylon
Normal file
31
Task/Web-scraping/Ceylon/web-scraping.ceylon
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import ceylon.uri {
|
||||
parse
|
||||
}
|
||||
import ceylon.http.client {
|
||||
get
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
// apparently the cgi link is deprecated?
|
||||
value oldUri = "http://tycho.usno.navy.mil/cgi-bin/timer.pl";
|
||||
value newUri = "http://tycho.usno.navy.mil/timer.pl";
|
||||
|
||||
value contents = downloadContents(newUri);
|
||||
value time = extractTime(contents);
|
||||
print(time else "nothing found");
|
||||
}
|
||||
|
||||
String downloadContents(String uriString) {
|
||||
value uri = parse(uriString);
|
||||
value request = get(uri);
|
||||
value response = request.execute();
|
||||
return response.contents;
|
||||
}
|
||||
|
||||
String? extractTime(String contents) =>
|
||||
contents
|
||||
.lines
|
||||
.filter((String element) => element.contains("UTC"))
|
||||
.first
|
||||
?.substring(4, 21);
|
||||
1
Task/Web-scraping/Clojure/web-scraping.clj
Normal file
1
Task/Web-scraping/Clojure/web-scraping.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(second (re-find #" (\d{1,2}:\d{1,2}:\d{1,2}) UTC" (slurp "http://tycho.usno.navy.mil/cgi-bin/timer.pl")))
|
||||
39
Task/Web-scraping/CoffeeScript/web-scraping.coffee
Normal file
39
Task/Web-scraping/CoffeeScript/web-scraping.coffee
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
http = require 'http'
|
||||
|
||||
CONFIG =
|
||||
host: 'tycho.usno.navy.mil'
|
||||
path: '/cgi-bin/timer.pl'
|
||||
|
||||
# Web scraping code tends to be brittle, and this is no exception.
|
||||
# The tycho time page does not use highly structured markup, so
|
||||
# we do a real dirty scrape.
|
||||
scrape_tycho_ust_time = (text) ->
|
||||
for line in text.split '\n'
|
||||
matches = line.match /(.*:\d\d UTC)/
|
||||
if matches
|
||||
console.log matches[0].replace '<BR>', ''
|
||||
return
|
||||
throw Error("unscrapable page!")
|
||||
|
||||
# This is low-level-ish code to get data from a URL. It's
|
||||
# pretty general purpose, so you'd normally tuck this away
|
||||
# in a library (or use somebody else's library).
|
||||
wget = (host, path, cb) ->
|
||||
options =
|
||||
host: host
|
||||
path: path
|
||||
headers:
|
||||
"Cache-Control": "max-age=0"
|
||||
|
||||
req = http.request options, (res) ->
|
||||
s = ''
|
||||
res.on 'data', (chunk) ->
|
||||
s += chunk
|
||||
res.on 'end', ->
|
||||
cb s
|
||||
req.end()
|
||||
|
||||
# Do our web scrape
|
||||
do ->
|
||||
wget CONFIG.host, CONFIG.path, (data) ->
|
||||
scrape_tycho_ust_time data
|
||||
10
Task/Web-scraping/Common-Lisp/web-scraping-1.lisp
Normal file
10
Task/Web-scraping/Common-Lisp/web-scraping-1.lisp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
BOA> (let* ((url "http://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
(regexp (load-time-value
|
||||
(cl-ppcre:create-scanner "(?m)^.{4}(.+? UTC)")))
|
||||
(data (drakma:http-request url)))
|
||||
(multiple-value-bind (start end start-regs end-regs)
|
||||
(cl-ppcre:scan regexp data)
|
||||
(declare (ignore end))
|
||||
(when start
|
||||
(subseq data (aref start-regs 0) (aref end-regs 0)))))
|
||||
"Aug. 12, 04:29:51 UTC"
|
||||
5
Task/Web-scraping/Common-Lisp/web-scraping-2.lisp
Normal file
5
Task/Web-scraping/Common-Lisp/web-scraping-2.lisp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
CL-USER> (cl-ppcre:do-matches-as-strings
|
||||
(m ".*<BR>(.*)UTC.*"
|
||||
(drakma:http-request "http://tycho.usno.navy.mil/cgi-bin/timer.pl"))
|
||||
(print (cl-ppcre:regex-replace "<BR>(.*UTC).*" m "\\1")))
|
||||
"Jul. 13, 06:32:01 UTC"
|
||||
7
Task/Web-scraping/D/web-scraping.d
Normal file
7
Task/Web-scraping/D/web-scraping.d
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
void main() {
|
||||
import std.stdio, std.string, std.net.curl, std.algorithm;
|
||||
|
||||
foreach (line; "http://tycho.usno.navy.mil/cgi-bin/timer.pl".byLine)
|
||||
if (line.canFind(" UTC"))
|
||||
line[4 .. $].writeln;
|
||||
}
|
||||
206
Task/Web-scraping/Delphi/web-scraping-1.delphi
Normal file
206
Task/Web-scraping/Delphi/web-scraping-1.delphi
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
program WebScrape;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{.$DEFINE DEBUG}
|
||||
|
||||
uses
|
||||
Classes,
|
||||
Winsock;
|
||||
|
||||
|
||||
{ Function to connect to host, send HTTP request and retrieve response }
|
||||
function DoHTTPGET(const hostName: PAnsiChar; const resource: PAnsiChar; HTTPResponse: TStrings): Boolean;
|
||||
const
|
||||
Port: integer = 80;
|
||||
CRLF = #13#10; // carriage return/line feed
|
||||
var
|
||||
WSAData: TWSAData;
|
||||
Sock: TSocket;
|
||||
SockAddrIn: TSockAddrIn;
|
||||
IPAddress: PHostEnt;
|
||||
bytesIn: integer;
|
||||
inBuffer: array [0..1023] of char;
|
||||
Req: string;
|
||||
begin
|
||||
Result := False;
|
||||
HTTPResponse.Clear;
|
||||
|
||||
{ Initialise use of the Windows Sockets DLL.
|
||||
Older Windows versions support Winsock 1.1 whilst newer Windows
|
||||
include Winsock 2 but support 1.1. Therefore, we'll specify
|
||||
version 1.1 ($101) as being the highest version of Windows Sockets
|
||||
that we can use to provide greatest flexibility.
|
||||
WSAData receives details of the Windows Sockets implementation }
|
||||
Winsock.WSAStartUp($101, WSAData);
|
||||
try
|
||||
|
||||
{ Create a socket for TCP/IP usage passing in
|
||||
Address family spec: AF_INET (TCP, UDP, etc.)
|
||||
Type specification: SOCK_STREAM
|
||||
Protocol: IPPROTO_TCP (TCP) }
|
||||
Sock := WinSock.Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
try
|
||||
|
||||
// Check we have a valid socket
|
||||
if (Sock <> INVALID_SOCKET) then
|
||||
begin
|
||||
// Populate socket address structure
|
||||
with SockAddrIn do
|
||||
begin
|
||||
// Address family specification
|
||||
sin_family := AF_INET;
|
||||
// Port
|
||||
sin_port := htons(Port);
|
||||
// Address
|
||||
sin_addr.s_addr := inet_addr(hostName);
|
||||
end;
|
||||
|
||||
if (SockAddrIn.sin_addr.s_addr = INADDR_NONE) then
|
||||
begin
|
||||
{ As we're using a domain name instead of an
|
||||
IP Address, we need to resolve the domain name }
|
||||
IPAddress := Winsock.gethostbyname(hostName);
|
||||
|
||||
// Quit if we didn't get an IP Address
|
||||
if (IPAddress = nil) then
|
||||
Exit;
|
||||
|
||||
// Update the structure with the IP Address
|
||||
SockAddrIn.sin_addr.s_addr := PLongint(IPAddress^.h_addr_list^)^;
|
||||
end;
|
||||
|
||||
// Try to connect to host
|
||||
if (Winsock.connect(Sock, SockAddrIn, SizeOf(SockAddrIn)) <> SOCKET_ERROR) then
|
||||
begin
|
||||
// OK - Connected
|
||||
|
||||
// Compose our request
|
||||
// Each line of the request must be terminated with a carriage return/line feed
|
||||
|
||||
{ The First line specifies method (e.g. GET, POST), path to required resource,
|
||||
and the HTTP version being used. These three fields are space separated. }
|
||||
Req := 'GET '+resource+' HTTP/1.1' + CRLF +
|
||||
|
||||
// Host: is the only Required header in HTTP 1.1
|
||||
'Host: '+hostName + CRLF +
|
||||
|
||||
{ Persistent connections are the default in HTTP 1.1 but, as we don't want
|
||||
or need one for this exercise, we must include the "Connection: close"
|
||||
header in our request }
|
||||
'Connection: close' + CRLF +
|
||||
|
||||
CRLF; // Request must end with an empty line!
|
||||
|
||||
// Try to send the request to the host
|
||||
if (Winsock.send(Sock,Req[1],Length(Req),0) <> SOCKET_ERROR) then
|
||||
begin
|
||||
// Initialise incoming data buffer (i.e. fill array with nulls)
|
||||
FillChar(inBuffer,SizeOf(inBuffer),#0);
|
||||
// Loop until nothing left to read
|
||||
repeat
|
||||
// Read incoming data from socket
|
||||
bytesIn := Winsock.recv(Sock, inBuffer, SizeOf(inBuffer), 0);
|
||||
// Assign buffer to Stringlist
|
||||
HTTPResponse.Text := HTTPResponse.Text + Copy(string(inBuffer),1,bytesIn);
|
||||
until
|
||||
(bytesIn <= 0) or (bytesIn = SOCKET_ERROR);
|
||||
|
||||
{ Our list of response strings should
|
||||
contain at least 1 line }
|
||||
Result := HTTPResponse.Count > 0;
|
||||
end;
|
||||
|
||||
end;
|
||||
|
||||
end;
|
||||
|
||||
finally
|
||||
// Close our socket
|
||||
Winsock.closesocket(Sock);
|
||||
end;
|
||||
|
||||
finally
|
||||
{ This causes our application to deregister itself from this
|
||||
Windows Sockets implementation and allows the implementation
|
||||
to free any resources allocated on our behalf. }
|
||||
Winsock.WSACleanup;
|
||||
end;
|
||||
|
||||
end;
|
||||
|
||||
{ Simple function to locate and return the UTC time from the
|
||||
request sent to http://tycho.usno.navy.mil/cgi-bin/timer.pl
|
||||
The HTTPResponse parameter contains both the HTTP Headers and
|
||||
the HTML served up by the requested resource. }
|
||||
function ParseResponse(HTTPResponse: TStrings): string;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
|
||||
{ Check first line for server response code
|
||||
We want something like this: HTTP/1.1 200 OK }
|
||||
if Pos('200',HTTPResponse[0]) > 0 then
|
||||
begin
|
||||
for i := 0 to Pred(HTTPResponse.Count) do
|
||||
begin
|
||||
{ The line we're looking for is something like this:
|
||||
<BR>May. 04. 21:55:19 UTC Universal Time }
|
||||
|
||||
// Check each line
|
||||
if Pos('UTC',HTTPResponse[i]) > 0 then
|
||||
begin
|
||||
Result := Copy(HTTPResponse[i],5,Pos('UTC',HTTPResponse[i])-1);
|
||||
Break;
|
||||
end;
|
||||
|
||||
end;
|
||||
end
|
||||
else
|
||||
Result := 'HTTP Error: '+HTTPResponse[0];
|
||||
end;
|
||||
|
||||
|
||||
const
|
||||
host: PAnsiChar = 'tycho.usno.navy.mil';
|
||||
res : PAnsiChar = '/cgi-bin/timer.pl';
|
||||
|
||||
|
||||
var
|
||||
Response: TStrings;
|
||||
|
||||
begin
|
||||
{ A TStringList is a TStrings descendant class
|
||||
that is used to store and manipulate a list
|
||||
of strings.
|
||||
|
||||
Instantiate a stringlist class to
|
||||
hold the results of our HTTP GET }
|
||||
Response := TStringList.Create;
|
||||
try
|
||||
// Try an HTTP GET request
|
||||
if DoHTTPGET(host,res,Response) then
|
||||
begin
|
||||
{$IFDEF DEBUG}
|
||||
{ Write the entire response to
|
||||
the console window }
|
||||
Writeln(Response.text);
|
||||
{$ELSE}
|
||||
{ Parse the response and write the
|
||||
result to the console window }
|
||||
Writeln(ParseResponse(Response));
|
||||
{$ENDIF DEBUG}
|
||||
end
|
||||
else
|
||||
Writeln('Error retrieving data');
|
||||
|
||||
finally
|
||||
Response.Free;
|
||||
end;
|
||||
|
||||
// Keep console window open
|
||||
Readln;
|
||||
|
||||
|
||||
end.
|
||||
28
Task/Web-scraping/Delphi/web-scraping-2.delphi
Normal file
28
Task/Web-scraping/Delphi/web-scraping-2.delphi
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
program ReadUTCTime;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils, Classes, IdHTTP;
|
||||
|
||||
var
|
||||
s: string;
|
||||
lHTTP: TIdHTTP;
|
||||
lReader: TStringReader;
|
||||
begin
|
||||
lHTTP := TIdHTTP.Create(nil);
|
||||
try
|
||||
lReader := TStringReader.Create(lHTTP.Get('http://tycho.usno.navy.mil/cgi-bin/timer.pl'));
|
||||
while lReader.Peek > 0 do
|
||||
begin
|
||||
s := lReader.ReadLine;
|
||||
if Pos('UTC', s) > 0 then
|
||||
begin
|
||||
Writeln(s);
|
||||
Break;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
lHTTP.Free;
|
||||
lReader.Free;
|
||||
end;
|
||||
end.
|
||||
4
Task/Web-scraping/E/web-scraping.e
Normal file
4
Task/Web-scraping/E/web-scraping.e
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
interp.waitAtTop(when (def html := <http://tycho.usno.navy.mil/cgi-bin/timer.pl>.getText()) -> {
|
||||
def rx`(?s).*>(@time.*? UTC).*` := html
|
||||
println(time)
|
||||
})
|
||||
10
Task/Web-scraping/Erlang/web-scraping.erl
Normal file
10
Task/Web-scraping/Erlang/web-scraping.erl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
-module(scraping).
|
||||
-export([main/0]).
|
||||
-define(Url, "http://tycho.usno.navy.mil/cgi-bin/timer.pl").
|
||||
-define(Match, "<BR>(.+ UTC)").
|
||||
|
||||
main() ->
|
||||
inets:start(),
|
||||
{ok, {_Status, _Header, HTML}} = httpc:request(?Url),
|
||||
{match, [Time]} = re:run(HTML, ?Match, [{capture, all_but_first, binary}]),
|
||||
io:format("~s~n",[Time]).
|
||||
11
Task/Web-scraping/F-Sharp/web-scraping.fs
Normal file
11
Task/Web-scraping/F-Sharp/web-scraping.fs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
open System
|
||||
open System.Net
|
||||
open System.Text.RegularExpressions
|
||||
|
||||
async {
|
||||
use wc = new WebClient()
|
||||
let! html = wc.AsyncDownloadString(Uri("http://tycho.usno.navy.mil/cgi-bin/timer.pl"))
|
||||
return Regex.Match(html, @"<BR>(.+ UTC)").Groups.[1].Value
|
||||
}
|
||||
|> Async.RunSynchronously
|
||||
|> printfn "%s"
|
||||
4
Task/Web-scraping/Factor/web-scraping.factor
Normal file
4
Task/Web-scraping/Factor/web-scraping.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
USING: http.client io sequences ;
|
||||
|
||||
"http://tycho.usno.navy.mil/cgi-bin/timer.pl" http-get nip
|
||||
[ "UTC" swap start [ 9 - ] [ 1 - ] bi ] keep subseq print
|
||||
15
Task/Web-scraping/Forth/web-scraping.fth
Normal file
15
Task/Web-scraping/Forth/web-scraping.fth
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
include unix/socket.fs
|
||||
|
||||
: extract-time ( addr len type len -- time len )
|
||||
dup >r
|
||||
search 0= abort" that time not present!"
|
||||
dup >r
|
||||
begin -1 /string over 1- c@ [char] > = until \ seek back to <BR> at start of line
|
||||
r> - r> + ;
|
||||
|
||||
s" tycho.usno.navy.mil" 80 open-socket
|
||||
dup s\" GET /cgi-bin/timer.pl HTTP/1.0\n\n" rot write-socket
|
||||
dup pad 4096 read-socket
|
||||
s\" \r\n\r\n" search 0= abort" can't find headers!" \ skip headers
|
||||
s" UTC" extract-time type cr
|
||||
close-socket
|
||||
5
Task/Web-scraping/FunL/web-scraping.funl
Normal file
5
Task/Web-scraping/FunL/web-scraping.funl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import io.Source
|
||||
|
||||
case Source.fromURL( 'http://tycho.usno.navy.mil/cgi-bin/timer.pl', 'UTF-8' ).getLines().find( ('Eastern' in) ) of
|
||||
Some( time ) -> println( time.substring(4) )
|
||||
None -> error( 'Easter time not found' )
|
||||
17
Task/Web-scraping/Gambas/web-scraping.gambas
Normal file
17
Task/Web-scraping/Gambas/web-scraping.gambas
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
Public Sub Main()
|
||||
Dim sWeb, sTemp, sOutput As String 'Variables
|
||||
|
||||
Shell "wget -O /tmp/web http://tycho.usno.navy.mil/cgi-bin/timer.pl" Wait 'Use 'wget' to save the web file in /tmp/
|
||||
|
||||
sWeb = File.Load("/tmp/web") 'Open file and store in sWeb
|
||||
|
||||
For Each sTemp In Split(sWeb, gb.NewLine) 'Split the file by NewLines..
|
||||
If InStr(sTemp, "UTC") Then 'If the line contains "UTC" then..
|
||||
sOutPut = sTemp 'Extract the line into sOutput
|
||||
Break 'Get out of here
|
||||
End If
|
||||
Next
|
||||
|
||||
Print Mid(sOutput, 5) 'Print the result without the '<BR>' tag
|
||||
|
||||
End
|
||||
56
Task/Web-scraping/Go/web-scraping.go
Normal file
56
Task/Web-scraping/Go/web-scraping.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resp, err := http.Get("http://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
if err != nil {
|
||||
fmt.Println(err) // connection or request fail
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var us string
|
||||
var ux int
|
||||
utc := []byte("UTC")
|
||||
for p := xml.NewDecoder(resp.Body); ; {
|
||||
t, err := p.RawToken()
|
||||
switch err {
|
||||
case nil:
|
||||
case io.EOF:
|
||||
fmt.Println("UTC not found")
|
||||
return
|
||||
default:
|
||||
fmt.Println(err) // read or parse fail
|
||||
return
|
||||
}
|
||||
if ub, ok := t.(xml.CharData); ok {
|
||||
if ux = bytes.Index(ub, utc); ux != -1 {
|
||||
// success: found a line with the string "UTC"
|
||||
us = string([]byte(ub))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// first thing to try: parsing the expected date format
|
||||
if t, err := time.Parse("Jan. 2, 15:04:05 UTC", us[:ux+3]); err == nil {
|
||||
fmt.Println("parsed UTC:", t.Format("January 2, 15:04:05"))
|
||||
return
|
||||
}
|
||||
// fallback: search for anything looking like a time and print that
|
||||
tx := regexp.MustCompile("[0-2]?[0-9]:[0-5][0-9]:[0-6][0-9]")
|
||||
if justTime := tx.FindString(us); justTime > "" {
|
||||
fmt.Println("found UTC:", justTime)
|
||||
return
|
||||
}
|
||||
// last resort: just print the whole element containing "UTC" and hope
|
||||
// there is a human readable time in there somewhere.
|
||||
fmt.Println(us)
|
||||
}
|
||||
8
Task/Web-scraping/Groovy/web-scraping.groovy
Normal file
8
Task/Web-scraping/Groovy/web-scraping.groovy
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def time = "unknown"
|
||||
def text = new URL('http://tycho.usno.navy.mil/cgi-bin/timer.pl').eachLine { line ->
|
||||
def matcher = (line =~ "<BR>(.+) UTC")
|
||||
if (matcher.find()) {
|
||||
time = matcher[0][1]
|
||||
}
|
||||
}
|
||||
println "UTC Time was '$time'"
|
||||
7
Task/Web-scraping/Haskell/web-scraping-1.hs
Normal file
7
Task/Web-scraping/Haskell/web-scraping-1.hs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import Data.List
|
||||
import Network.HTTP (simpleHTTP, getResponseBody, getRequest)
|
||||
|
||||
tyd = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
|
||||
readUTC = simpleHTTP (getRequest tyd)>>=
|
||||
fmap ((!!2).head.dropWhile ("UTC"`notElem`).map words.lines). getResponseBody>>=putStrLn
|
||||
2
Task/Web-scraping/Haskell/web-scraping-2.hs
Normal file
2
Task/Web-scraping/Haskell/web-scraping-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*Main> readUTC
|
||||
08:30:23
|
||||
4
Task/Web-scraping/J/web-scraping.j
Normal file
4
Task/Web-scraping/J/web-scraping.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
require 'web/gethttp'
|
||||
|
||||
_8{. ' UTC' taketo gethttp 'http://tycho.usno.navy.mil/cgi-bin/timer.pl'
|
||||
04:32:44
|
||||
15
Task/Web-scraping/Java/web-scraping-1.java
Normal file
15
Task/Web-scraping/Java/web-scraping-1.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
String scrapeUTC() throws URISyntaxException, IOException {
|
||||
String address = "http://tycho.usno.navy.mil/cgi-bin/timer.pl";
|
||||
URL url = new URI(address).toURL();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
|
||||
Pattern pattern = Pattern.compile("^.+? UTC");
|
||||
Matcher matcher;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
matcher = pattern.matcher(line);
|
||||
if (matcher.find())
|
||||
return matcher.group().replaceAll("<.+?>", "");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
38
Task/Web-scraping/Java/web-scraping-2.java
Normal file
38
Task/Web-scraping/Java/web-scraping-2.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
public final class WebScraping {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
try {
|
||||
URI uri = new URI("https://www.rosettacode.org/wiki/Talk:Web_scraping").parseServerAuthority();
|
||||
URL address = uri.toURL();
|
||||
HttpURLConnection connection = (HttpURLConnection) address.openConnection();
|
||||
BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()) );
|
||||
|
||||
final int responseCode = connection.getResponseCode();
|
||||
System.out.println("Response code: " + responseCode);
|
||||
|
||||
String line;
|
||||
while ( ! ( line = reader.readLine() ).contains("UTC") ) {
|
||||
/* Empty block */
|
||||
}
|
||||
|
||||
final int index = line.indexOf("UTC");
|
||||
System.out.println(line.substring(index - 16, index + 4));
|
||||
|
||||
reader.close();
|
||||
connection.disconnect();
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("Error connecting to server: " + ioe.getCause());
|
||||
} catch (URISyntaxException use) {
|
||||
System.err.println("Unable to connect to URI: " + use.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
7
Task/Web-scraping/JavaScript/web-scraping.js
Normal file
7
Task/Web-scraping/JavaScript/web-scraping.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
var req = new XMLHttpRequest();
|
||||
req.onload = function () {
|
||||
var re = /[JFMASOND].+ UTC/; //beginning of month name to 'UTC'
|
||||
console.log(this.responseText.match(re)[0]);
|
||||
};
|
||||
req.open('GET', 'http://tycho.usno.navy.mil/cgi-bin/timer.pl', true);
|
||||
req.send();
|
||||
3
Task/Web-scraping/Jq/web-scraping-1.jq
Normal file
3
Task/Web-scraping/Jq/web-scraping-1.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
curl -Ss 'http://tycho.usno.navy.mil/cgi-bin/timer.pl' |\
|
||||
jq -R -r 'if index(" UTC") then .[4:] else empty end'
|
||||
2
Task/Web-scraping/Jq/web-scraping-2.jq
Normal file
2
Task/Web-scraping/Jq/web-scraping-2.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$ ./Web_scraping.jq
|
||||
Apr. 21, 05:19:32 UTC Universal Time
|
||||
22
Task/Web-scraping/Julia/web-scraping-1.julia
Normal file
22
Task/Web-scraping/Julia/web-scraping-1.julia
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using Requests, Printf
|
||||
|
||||
function getusnotime()
|
||||
const url = "http://tycho.usno.navy.mil/timer.pl"
|
||||
s = try
|
||||
get(url)
|
||||
catch err
|
||||
@sprintf "get(%s)\n => %s" url err
|
||||
end
|
||||
isa(s, Requests.Response) || return (s, false)
|
||||
t = match(r"(?<=<BR>)(.*?UTC)", readstring(s))
|
||||
isa(t, RegexMatch) || return (@sprintf("raw html:\n %s", readstring(s)), false)
|
||||
return (t.match, true)
|
||||
end
|
||||
|
||||
(t, issuccess) = getusnotime();
|
||||
|
||||
if issuccess
|
||||
println("The USNO time is ", t)
|
||||
else
|
||||
println("Failed to fetch UNSO time:\n", t)
|
||||
end
|
||||
17
Task/Web-scraping/Julia/web-scraping-2.julia
Normal file
17
Task/Web-scraping/Julia/web-scraping-2.julia
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
Failed to fetch UNSO time:
|
||||
raw html:
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final"//EN>
|
||||
<html>
|
||||
<body>
|
||||
<TITLE>What time is it?</TITLE>
|
||||
<H2> US Naval Observatory Master Clock Time</H2> <H3><PRE>
|
||||
<BR>Apr. 20, 17:55:31 UTC Universal Time
|
||||
<BR>Apr. 20, 01:55:31 PM EDT Eastern Time
|
||||
<BR>Apr. 20, 12:55:31 PM CDT Central Time
|
||||
<BR>Apr. 20, 11:55:31 AM MDT Mountain Time
|
||||
<BR>Apr. 20, 10:55:31 AM PDT Pacific Time
|
||||
<BR>Apr. 20, 09:55:31 AM AKDT Alaska Time
|
||||
<BR>Apr. 20, 07:55:31 AM HAST Hawaii-Aleutian Time
|
||||
</PRE></H3><P><A HREF="http://www.usno.navy.mil"> US Naval Observatory</A>
|
||||
|
||||
</body></html>
|
||||
19
Task/Web-scraping/Kotlin/web-scraping.kotlin
Normal file
19
Task/Web-scraping/Kotlin/web-scraping.kotlin
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// version 1.1.3
|
||||
|
||||
import java.net.URL
|
||||
import java.io.InputStreamReader
|
||||
import java.util.Scanner
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val url = URL("http://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
val isr = InputStreamReader(url.openStream())
|
||||
val sc = Scanner(isr)
|
||||
while (sc.hasNextLine()) {
|
||||
val line = sc.nextLine()
|
||||
if ("UTC" in line) {
|
||||
println(line.drop(4).take(17))
|
||||
break
|
||||
}
|
||||
}
|
||||
sc.close()
|
||||
}
|
||||
27
Task/Web-scraping/Lasso/web-scraping.lasso
Normal file
27
Task/Web-scraping/Lasso/web-scraping.lasso
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* have to be used
|
||||
local(raw_htmlstring = '<TITLE>What time is it?</TITLE>
|
||||
<H2> US Naval Observatory Master Clock Time</H2> <H3><PRE>
|
||||
<BR>Jul. 27, 22:57:22 UTC Universal Time
|
||||
<BR>Jul. 27, 06:57:22 PM EDT Eastern Time
|
||||
<BR>Jul. 27, 05:57:22 PM CDT Central Time
|
||||
<BR>Jul. 27, 04:57:22 PM MDT Mountain Time
|
||||
<BR>Jul. 27, 03:57:22 PM PDT Pacific Time
|
||||
<BR>Jul. 27, 02:57:22 PM AKDT Alaska Time
|
||||
<BR>Jul. 27, 12:57:22 PM HAST Hawaii-Aleutian Time
|
||||
</PRE></H3>
|
||||
')
|
||||
*/
|
||||
|
||||
// should be used
|
||||
local(raw_htmlstring = string(include_url('http://tycho.usno.navy.mil/cgi-bin/timer.pl')))
|
||||
|
||||
local(
|
||||
reg_exp = regexp(-find = `<br>(.*?) UTC`, -input = #raw_htmlstring, -ignorecase),
|
||||
datepart_txt = #reg_exp -> find ? #reg_exp -> matchstring(1) | string
|
||||
)
|
||||
|
||||
#datepart_txt
|
||||
'<br />'
|
||||
// added bonus showing how parsed string can be converted to date object
|
||||
local(mydate = date(#datepart_txt, -format = `MMM'.' dd',' HH:mm:ss`))
|
||||
#mydate -> format(`YYYY-MM-dd HH:mm:ss`)
|
||||
23
Task/Web-scraping/Liberty-BASIC/web-scraping.basic
Normal file
23
Task/Web-scraping/Liberty-BASIC/web-scraping.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
if DownloadToFile("http://tycho.usno.navy.mil/cgi-bin/timer.pl", DefaultDir$ + "\timer.htm") = 0 then
|
||||
open DefaultDir$ + "\timer.htm" for input as #f
|
||||
html$ = lower$(input$(#f, LOF(#f)))
|
||||
close #f
|
||||
|
||||
a= instr( html$, "utc" )-1
|
||||
print "UTC";mid$( html$, a-9,9)
|
||||
|
||||
end if
|
||||
|
||||
end
|
||||
|
||||
function DownloadToFile(urlfile$, localfile$)
|
||||
open "URLmon" for dll as #url
|
||||
calldll #url, "URLDownloadToFileA",_
|
||||
0 as long,_ 'null
|
||||
urlfile$ as ptr,_ 'url to download
|
||||
localfile$ as ptr,_ 'save file name
|
||||
0 as long,_ 'reserved, must be 0
|
||||
0 as long,_ 'callback address, can be 0
|
||||
DownloadToFile as ulong '0=success
|
||||
close #url
|
||||
end function
|
||||
14
Task/Web-scraping/Lua/web-scraping.lua
Normal file
14
Task/Web-scraping/Lua/web-scraping.lua
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
local http = require("socket.http") -- Debian package is 'lua-socket'
|
||||
|
||||
function scrapeTime (pageAddress, timeZone)
|
||||
local page = http.request(pageAddress)
|
||||
if not page then return "Cannot connect" end
|
||||
for line in page:gmatch("[^<BR>]*") do
|
||||
if line:match(timeZone) then
|
||||
return line:match("%d+:%d+:%d+")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local url = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
print(scrapeTime(url, "UTC"))
|
||||
29
Task/Web-scraping/M2000-Interpreter/web-scraping.m2000
Normal file
29
Task/Web-scraping/M2000-Interpreter/web-scraping.m2000
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
Module Web_scraping {
|
||||
Print "Web scraping"
|
||||
function GetTime$(a$, what$="UTC") {
|
||||
document a$ ' change string to document
|
||||
find a$, what$ ' place data to stack
|
||||
Read find_pos
|
||||
if find_pos>0 then
|
||||
read par_order, par_pos
|
||||
b$=paragraph$(a$, par_order)
|
||||
k=instr(b$,">")
|
||||
if k>0 then if k<par_pos then b$=mid$(b$,k+1) :par_pos-=k
|
||||
k=rinstr(b$,"<")
|
||||
if k>0 then if k>par_pos then b$=Left(b$,k-1)
|
||||
=b$
|
||||
end if
|
||||
}
|
||||
declare msxml2 "MSXML2.XMLHTTP.6.0"
|
||||
rem print type$(msxml2)="IXMLHTTPRequest"
|
||||
Url$ = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
try ok {
|
||||
method msxml2, "Open", "GET", url$, false
|
||||
method msxml2,"Send"
|
||||
with msxml2,"responseText" as txt$
|
||||
Print GetTime$(txt$)
|
||||
}
|
||||
If error or not ok then Print Error$
|
||||
declare msxml2 nothing
|
||||
}
|
||||
Web_scraping
|
||||
8
Task/Web-scraping/MATLAB/web-scraping.m
Normal file
8
Task/Web-scraping/MATLAB/web-scraping.m
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
s = urlread('http://tycho.usno.navy.mil/cgi-bin/timer.pl');
|
||||
ix = [findstr(s,'<BR>'), length(s)+1];
|
||||
for k = 2:length(ix)
|
||||
tok = s(ix(k-1)+4:ix(k)-1);
|
||||
if findstr(tok,'UTC')
|
||||
disp(tok);
|
||||
end;
|
||||
end;
|
||||
23
Task/Web-scraping/MIRC-Scripting-Language/web-scraping.mirc
Normal file
23
Task/Web-scraping/MIRC-Scripting-Language/web-scraping.mirc
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
alias utc {
|
||||
sockclose UTC
|
||||
sockopen UTC tycho.usno.navy.mil 80
|
||||
}
|
||||
|
||||
on *:SOCKOPEN:UTC: {
|
||||
sockwrite -n UTC GET /cgi-bin/timer.pl HTTP/1.1
|
||||
sockwrite -n UTC Host: tycho.usno.navy.mil
|
||||
sockwrite UTC $crlf
|
||||
}
|
||||
|
||||
on *:SOCKREAD:UTC: {
|
||||
sockread %UTC
|
||||
while ($sockbr) {
|
||||
if (<BR>*Universal Time iswm %UTC) {
|
||||
echo -ag $remove(%UTC,<BR>,$chr(9),Universal Time)
|
||||
unset %UTC
|
||||
sockclose UTC
|
||||
return
|
||||
}
|
||||
sockread %UTC
|
||||
}
|
||||
}
|
||||
2
Task/Web-scraping/Maple/web-scraping.maple
Normal file
2
Task/Web-scraping/Maple/web-scraping.maple
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
text := URL:-Get("http://tycho.usno.navy.mil/cgi-bin/timer.pl"):
|
||||
printf(StringTools:-StringSplit(text,"<BR>")[2]);
|
||||
2
Task/Web-scraping/Mathematica/web-scraping.math
Normal file
2
Task/Web-scraping/Mathematica/web-scraping.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
test = StringSplit[Import["http://tycho.usno.navy.mil/cgi-bin/timer.pl"], "\n"];
|
||||
Extract[test, Flatten@Position[StringFreeQ[test, "UTC"], False]]
|
||||
14
Task/Web-scraping/Microsoft-Small-Basic/web-scraping.basic
Normal file
14
Task/Web-scraping/Microsoft-Small-Basic/web-scraping.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
'Entered by AykayayCiti -- Earl L. Montgomery
|
||||
url_name = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
url_data = Network.GetWebPageContents(url_name)
|
||||
find = "UTC"
|
||||
' the length from the UTC to the time is -18 so we need
|
||||
' to subtract from the UTC position
|
||||
pos = Text.GetIndexOf(url_data,find)-18
|
||||
result = Text.GetSubText(url_data,pos,(18+3)) 'plus 3 to add the UTC
|
||||
TextWindow.WriteLine(result)
|
||||
|
||||
'you can eleminate a line of code by putting the
|
||||
' GetIndexOf insde the GetSubText
|
||||
'result2 = Text.GetSubText(url_data,Text.GetIndexOf(url_data,find)-18,(18+3))
|
||||
'TextWindow.WriteLine(result2)
|
||||
36
Task/Web-scraping/NetRexx/web-scraping.netrexx
Normal file
36
Task/Web-scraping/NetRexx/web-scraping.netrexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols binary
|
||||
|
||||
parse arg full_short .
|
||||
if 'FULL'.abbrev(full_short.upper(), 1) then
|
||||
dateFull = isTrue()
|
||||
else
|
||||
dateFull = isFalse()
|
||||
do
|
||||
timeURL = java.net.URL('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
|
||||
conn = timeURL.openConnection()
|
||||
ibr = BufferedReader(InputStreamReader(conn.getInputStream()))
|
||||
line = Rexx
|
||||
loop label readLoop while ibr.ready()
|
||||
line = ibr.readLine()
|
||||
if line = null then leave readLoop
|
||||
line = line.translate(' ', '\t')
|
||||
if line.wordpos('UTC') > 0 then do
|
||||
parse line . '>' udatetime 'UTC' . -
|
||||
0 . '>' . ',' utime 'UTC' .
|
||||
if dateFull then
|
||||
say udatetime.strip() 'UTC'
|
||||
else
|
||||
say utime.strip()
|
||||
leave readLoop
|
||||
end
|
||||
end readLoop
|
||||
ibr.close()
|
||||
catch ex = IOException
|
||||
ex.printStackTrace()
|
||||
end
|
||||
|
||||
method isTrue() public constant returns boolean
|
||||
return 1 == 1
|
||||
method isFalse() public constant returns boolean
|
||||
return \isTrue()
|
||||
13
Task/Web-scraping/Nim/web-scraping.nim
Normal file
13
Task/Web-scraping/Nim/web-scraping.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import httpclient, strutils
|
||||
|
||||
var client = newHttpClient()
|
||||
|
||||
var res: string
|
||||
for line in client.getContent("https://rosettacode.org/wiki/Talk:Web_scraping").splitLines:
|
||||
let k = line.find("UTC")
|
||||
if k >= 0:
|
||||
res = line[0..(k - 3)]
|
||||
let k = res.rfind("</a>")
|
||||
res = res[(k + 6)..^1]
|
||||
break
|
||||
echo if res.len > 0: res else: "No date/time found."
|
||||
14
Task/Web-scraping/OCaml/web-scraping.ocaml
Normal file
14
Task/Web-scraping/OCaml/web-scraping.ocaml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
let () =
|
||||
let _,_, page_content = make_request ~url:Sys.argv.(1) ~kind:GET () in
|
||||
|
||||
let lines = Str.split (Str.regexp "\n") page_content in
|
||||
let str =
|
||||
List.find
|
||||
(fun line ->
|
||||
try ignore(Str.search_forward (Str.regexp "UTC") line 0); true
|
||||
with Not_found -> false)
|
||||
lines
|
||||
in
|
||||
let str = Str.global_replace (Str.regexp "<BR>") "" str in
|
||||
print_endline str;
|
||||
;;
|
||||
25
Task/Web-scraping/Objeck/web-scraping.objeck
Normal file
25
Task/Web-scraping/Objeck/web-scraping.objeck
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use Net;
|
||||
use IO;
|
||||
use Structure;
|
||||
|
||||
bundle Default {
|
||||
class Scrape {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
client := HttpClient->New();
|
||||
lines := client->Get("http://tycho.usno.navy.mil/cgi-bin/timer.pl", 80);
|
||||
|
||||
i := 0;
|
||||
found := false;
|
||||
while(found <> true & i < lines->Size()) {
|
||||
line := lines->Get(i)->As(String);
|
||||
index := line->Find("UTC");
|
||||
if(index > -1) {
|
||||
time := line->SubString(index - 9, 9)->Trim();
|
||||
time->PrintLine();
|
||||
found := true;
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Task/Web-scraping/OoRexx/web-scraping.rexx
Normal file
23
Task/Web-scraping/OoRexx/web-scraping.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* load the RexxcURL library */
|
||||
Call RxFuncAdd 'CurlLoadFuncs', 'rexxcurl', 'CurlLoadFuncs'
|
||||
Call CurlLoadFuncs
|
||||
|
||||
url = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
|
||||
/* get a curl session */
|
||||
curl = CurlInit()
|
||||
if curl \= ''
|
||||
then do
|
||||
call CurlSetopt curl, 'URL', Url
|
||||
if curlerror.intcode \= 0 then exit
|
||||
call curlSetopt curl, 'OUTSTEM', 'stem.'
|
||||
if curlerror.intcode \= 0 then exit
|
||||
call CurlPerform curl
|
||||
|
||||
/* content is in a stem - lets get it all in a string */
|
||||
content = stem.~allItems~makestring('l')
|
||||
/* now parse out utc time */
|
||||
parse var content content 'Universal Time' .
|
||||
utcTime = content~substr(content~lastpos('<BR>') + 4)
|
||||
say utcTime
|
||||
end
|
||||
20
Task/Web-scraping/Oz/web-scraping.oz
Normal file
20
Task/Web-scraping/Oz/web-scraping.oz
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
declare
|
||||
[Regex] = {Module.link ['x-oz://contrib/regex']}
|
||||
|
||||
fun {GetPage Url}
|
||||
F = {New Open.file init(url:Url)}
|
||||
Contents = {F read(list:$ size:all)}
|
||||
in
|
||||
{F close}
|
||||
Contents
|
||||
end
|
||||
|
||||
fun {GetDateString Doc}
|
||||
case {Regex.search "<BR>([A-Za-z0-9:., ]+ UTC)" Doc}
|
||||
of match(1:S#E ...) then {List.take {List.drop Doc S} E-S+1}
|
||||
end
|
||||
end
|
||||
|
||||
Url = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
|
||||
in
|
||||
{System.showInfo {GetDateString {GetPage Url}}}
|
||||
8
Task/Web-scraping/PHP/web-scraping-1.php
Normal file
8
Task/Web-scraping/PHP/web-scraping-1.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?
|
||||
|
||||
$contents = file('http://tycho.usno.navy.mil/cgi-bin/timer.pl');
|
||||
foreach ($contents as $line){
|
||||
if (($pos = strpos($line, ' UTC')) === false) continue;
|
||||
echo subStr($line, 4, $pos - 4); //Prints something like "Dec. 06, 16:18:03"
|
||||
break;
|
||||
}
|
||||
7
Task/Web-scraping/PHP/web-scraping-2.php
Normal file
7
Task/Web-scraping/PHP/web-scraping-2.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?
|
||||
|
||||
echo preg_replace(
|
||||
"/^.*<BR>(.*) UTC.*$/su",
|
||||
"\\1",
|
||||
file_get_contents('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
|
||||
);
|
||||
5
Task/Web-scraping/Peloton/web-scraping-1.peloton
Normal file
5
Task/Web-scraping/Peloton/web-scraping-1.peloton
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<@ DEFAREPRS>Rexx Parse</@>
|
||||
<@ DEFPRSLIT>Rexx Parse|'<BR>' UTCtime 'UTC'</@>
|
||||
<@ LETVARURL>timer|http://tycho.usno.navy.mil/cgi-bin/timer.pl</@>
|
||||
<@ ACTRPNPRSVAR>Rexx Parse|timer</@>
|
||||
<@ SAYVAR>UTCtime</@>
|
||||
5
Task/Web-scraping/Peloton/web-scraping-2.peloton
Normal file
5
Task/Web-scraping/Peloton/web-scraping-2.peloton
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<# DEFINE WORKAREA PARSEVALUES>Rexx Parse</#>
|
||||
<# DEFINE PARSEVALUES LITERAL>Rexx Parse|'<BR>' UTCtime 'UTC'</#>
|
||||
<# LET VARIABLE URLSOURCE>timer|http://tycho.usno.navy.mil/cgi-bin/timer.pl</#>
|
||||
<# ACT REPLACEBYPATTERN PARSEVALUES VARIABLE>Rexx Parse|timer</#>
|
||||
<# SAY VARIABLE>UTCtime</#>
|
||||
1
Task/Web-scraping/Peloton/web-scraping-3.peloton
Normal file
1
Task/Web-scraping/Peloton/web-scraping-3.peloton
Normal file
|
|
@ -0,0 +1 @@
|
|||
<@ SAY AFT BEF URL LIT LIT LIT >http://tycho.usno.navy.mil/cgi-bin/timer.pl| UTC|<BR></@>
|
||||
5
Task/Web-scraping/Perl/web-scraping.pl
Normal file
5
Task/Web-scraping/Perl/web-scraping.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use LWP::Simple;
|
||||
|
||||
my $url = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl';
|
||||
get($url) =~ /<BR>(.+? UTC)/
|
||||
and print "$1\n";
|
||||
33
Task/Web-scraping/Phix/web-scraping.phix
Normal file
33
Task/Web-scraping/Phix/web-scraping.phix
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
(notonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\web_scrape.exw
|
||||
-- ===========================
|
||||
--</span>
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (libcurl)</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">curl_easy_perform_ex</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"https://rosettacode.org/wiki/Talk:Web_scraping"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`<div id="siteNotice">`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (24/11/22, exclude notice)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"UTC"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (debug aid)</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rmatch</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"</a>"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">..$])</span>
|
||||
<span style="color: #008080;">exit</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">timedate</span> <span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">parse_date_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"hh:mm, d Mmmm yyyy"</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">td</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dddd Mmmm ddth yyyy h:mpm"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"some error"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">curl_easy_strerror</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<!--
|
||||
5
Task/Web-scraping/PicoLisp/web-scraping.l
Normal file
5
Task/Web-scraping/PicoLisp/web-scraping.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(load "@lib/http.l")
|
||||
|
||||
(client "tycho.usno.navy.mil" 80 "cgi-bin/timer.pl"
|
||||
(when (from "<BR>")
|
||||
(pack (trim (till "U"))) ) )
|
||||
4
Task/Web-scraping/PowerShell/web-scraping-1.psh
Normal file
4
Task/Web-scraping/PowerShell/web-scraping-1.psh
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$wc = New-Object Net.WebClient
|
||||
$html = $wc.DownloadString('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
|
||||
$html -match ', (.*) UTC' | Out-Null
|
||||
Write-Host $Matches[1]
|
||||
1
Task/Web-scraping/PowerShell/web-scraping-2.psh
Normal file
1
Task/Web-scraping/PowerShell/web-scraping-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
[System.DateTime]::UtcNow
|
||||
1
Task/Web-scraping/PowerShell/web-scraping-3.psh
Normal file
1
Task/Web-scraping/PowerShell/web-scraping-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
[System.DateTime]::Now
|
||||
4
Task/Web-scraping/PureBasic/web-scraping.basic
Normal file
4
Task/Web-scraping/PureBasic/web-scraping.basic
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
URLDownloadToFile_( #Null, "http://tycho.usno.navy.mil/cgi-bin/timer.pl", "timer.htm", 0, #Null)
|
||||
ReadFile(0, "timer.htm")
|
||||
While Not Eof(0) : Text$ + ReadString(0) : Wend
|
||||
MessageRequester("Time", Mid(Text$, FindString(Text$, "UTC", 1) - 9 , 8))
|
||||
7
Task/Web-scraping/Python/web-scraping.py
Normal file
7
Task/Web-scraping/Python/web-scraping.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import urllib
|
||||
page = urllib.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
|
||||
for line in page:
|
||||
if ' UTC' in line:
|
||||
print line.strip()[4:]
|
||||
break
|
||||
page.close()
|
||||
4
Task/Web-scraping/R/web-scraping-1.r
Normal file
4
Task/Web-scraping/R/web-scraping-1.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
all_lines <- readLines("http://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
utc_line <- grep("UTC", all_lines, value = TRUE)
|
||||
matched <- regexpr("(\\w{3}.*UTC)", utc_line)
|
||||
utc_time_str <- substring(line, matched, matched + attr(matched, "match.length") - 1L)
|
||||
3
Task/Web-scraping/R/web-scraping-2.r
Normal file
3
Task/Web-scraping/R/web-scraping-2.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
library(stringr)
|
||||
utc_line <- all_lines[str_detect(all_lines, "UTC")]
|
||||
utc_time_str <- str_extract(utc_line, "\\w{3}.*UTC")
|
||||
2
Task/Web-scraping/R/web-scraping-3.r
Normal file
2
Task/Web-scraping/R/web-scraping-3.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
utc_time <- strptime(utc_time_str, "%b. %d, %H:%M:%S UTC")
|
||||
strftime(utc_time, "%A, %d %B %Y, %H:%M:%S")
|
||||
2
Task/Web-scraping/R/web-scraping-4.r
Normal file
2
Task/Web-scraping/R/web-scraping-4.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
library(RCurl)
|
||||
web_page <- getURL("http://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
5
Task/Web-scraping/R/web-scraping-5.r
Normal file
5
Task/Web-scraping/R/web-scraping-5.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
library(XML)
|
||||
page_tree <- htmlTreeParse(webpage)
|
||||
times_node <- page_tree$children$html$children$body$children$h3$children$pre$children
|
||||
times_node <- times_node[names(times_node) == "text"]
|
||||
time_lines <- sapply(times_node, function(x) x$value)
|
||||
4
Task/Web-scraping/R/web-scraping-6.r
Normal file
4
Task/Web-scraping/R/web-scraping-6.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
page_tree <- htmlTreeParse(web_page, useInternalNodes = TRUE)
|
||||
times_node <- xpathSApply(page_tree, "//pre")[[1]]
|
||||
times_node <- times_node[names(times_node) == "text"]
|
||||
time_lines <- sapply(times_node, function(x) as(x, "character"))
|
||||
2
Task/Web-scraping/R/web-scraping-7.r
Normal file
2
Task/Web-scraping/R/web-scraping-7.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
utc_line <- time_lines[str_detect(time_lines, "UTC")]
|
||||
#etc.
|
||||
25
Task/Web-scraping/REBOL/web-scraping.rebol
Normal file
25
Task/Web-scraping/REBOL/web-scraping.rebol
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
REBOL [
|
||||
Title: "Web Scraping"
|
||||
URL: http://rosettacode.org/wiki/Web_Scraping
|
||||
]
|
||||
|
||||
; Notice that REBOL understands unquoted URL's:
|
||||
|
||||
service: http://tycho.usno.navy.mil/cgi-bin/timer.pl
|
||||
|
||||
; The 'read' function can read from any data scheme that REBOL knows
|
||||
; about, which includes web URLs. NOTE: Depending on your security
|
||||
; settings, REBOL may ask you for permission to contact the service.
|
||||
|
||||
html: read service
|
||||
|
||||
; I parse the HTML to find the first <br> (note the unquoted HTML tag
|
||||
; -- REBOL understands those too), then copy the current time from
|
||||
; there to the "UTC" terminator.
|
||||
|
||||
; I have the "to end" in the parse rule so the parse will succeed.
|
||||
; Not strictly necessary once I've got the time, but good practice.
|
||||
|
||||
parse html [thru <br> copy current thru "UTC" to end]
|
||||
|
||||
print ["Current UTC time:" current]
|
||||
5
Task/Web-scraping/Racket/web-scraping.rkt
Normal file
5
Task/Web-scraping/Racket/web-scraping.rkt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#lang racket
|
||||
(require net/url)
|
||||
((compose1 car (curry regexp-match #rx"[^ <>][^<>]+ UTC")
|
||||
port->string get-pure-port string->url)
|
||||
"https://tycho.usno.navy.mil/cgi-bin/timer.pl")
|
||||
21
Task/Web-scraping/Raku/web-scraping.raku
Normal file
21
Task/Web-scraping/Raku/web-scraping.raku
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# 20210301 Updated Raku programming solution
|
||||
|
||||
use HTTP::Client; # https://github.com/supernovus/perl6-http-client/
|
||||
|
||||
#`[ Site inaccessible since 2019 ? | ||||