» »

[C#] Prosim pomagajte! Potrebujem program, ki bi pobiral podatke iz ene strani

[C#] Prosim pomagajte! Potrebujem program, ki bi pobiral podatke iz ene strani

David1994 ::

Rad bi napravil program, ki bi mi iz spletne strani pobral 3 stringe (oznacene na sliki).
Pred kratkim sem tle ze tezil za source pobirat, vendar mi ni prav uspelo:\



Hvala v naprej!

LP, David:D

jure1825 ::

ja odpreš stran pol pa greš čez vse div-e in poiščeš tistega ki ima id=1, 2 in 3.
sej vidiš da je v sourceu:
div id="1" ...

Kostko ::

V Pythonu lahko narediš skoraj v eni vrstici:
import urllib2, re
re.compile('<div id="[123]" .*?nowrap>(.*?)</div>').findall(urllib2.urlopen('http://www.abancna-dzu.si/vipek.asp').read())

To vrne seznam vseh treh vrednosti, trenutno ['5,14', '13.07.2007', '1,05']. V drugih jezikih podobno, ključen je tist regularni izraz tam... ;)
Human stupidity is not convergent, it has no limit!

StratOS ::

Če že hočeš kr kreacijo out.txt z podatki je rešitev tukaj
Drugače pa sploh ni problem iskati takšne podatke.
<div id="1" style="position: absolute; visibility: visible; left: 602px; top: 209px; color: #009900; width:50px; text-align:right;" nowrap>5,14</div>
<div id="2" style="position: absolute; visibility: visible; left: 670px; top: 209px; color: #009900; width:50px; text-align:right;" nowrap>13.07.2007</div>
<div id="3" style="position: absolute; visibility: visible; left: 736px; top: 209px; color: #009900; width:36px; text-align:right;" nowrap>1,05</div>
"Multitasking - ability to f##k up several things at once."
"It works better if you plug it in."
"The one who is digging the hole for the other to fall in is allready in it."

David1994 ::

StratOS, tvoja resitev je super, a bi mi lahko poslal source, saj bi rad naredil, da bi mi avtomatsko preračunalo, koliko je vrednost vsega skupaj, kar imam. Namrec, ne bi rad uporabljal streamreaderja.

LP, David:D

StratOS ::

Hja, ni v C-ju ampak v VB-ju, napisana na hitro ...
Uporablja par API-jev.
No source lahko vseeno prilepim :

Attribute VB_Name = "GetUrl"
Option Explicit
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

Public Const IF_FROM_CACHE = &H1000000
Public Const IF_MAKE_PERSISTENT = &H2000000
Public Const IF_NO_CACHE_WRITE = &H4000000
       
Private Const BUFFER_LEN = 256


Public Function GetUrlSource(sURL As String) As String
    Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    Dim hInternet As Long, hSession As Long, lReturn As Long

    'get the handle of the current internet connection
    hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    'get the handle of the url
    If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    'if we have the handle, then start reading the web page
    If hInternet Then
        'get the first chunk & buffer it.
        iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
        sData = sBuffer
        'if there's more data then keep reading it into the buffer
        Do While lReturn <> 0
            iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
            sData = sData + Mid(sBuffer, 1, lReturn)
        Loop
    End If
   
    'close the URL
    iResult = InternetCloseHandle(hInternet)

    GetUrlSource = sData
End Function

Sub Main()
Dim i As Long
Dim i1 As Long
Dim Tmp As String
Dim S(2) As String

Const f1 As String = "<div id=" & """" & "1" & """" & " style=" & """" & "position: absolute; visibility: visible; left: 602px; top: 209px; color: #009900; width:50px; text-align:right;" & """" & " nowrap>"
Const f2 As String = "<div id=" & """" & "2" & """" & " style=" & """" & "position: absolute; visibility: visible; left: 670px; top: 209px; color: #009900; width:50px; text-align:right;" & """" & " nowrap>"
Const f3 As String = "<div id=" & """" & "3" & """" & " style=" & """" & "position: absolute; visibility: visible; left: 736px; top: 209px; color: #009900; width:36px; text-align:right;" & """" & " nowrap>"

Const f4 As String = "</div>"
Tmp = GetUrlSource("http://www.abancna-dzu.si/vipek.asp")

i = InStr(1, Tmp, f1)
i1 = InStr(i + 1, Tmp, f4)

S(0) = Mid$(Tmp, i + Len(f1), i1 - i - Len(f1))

i = InStr(i1 + 1, Tmp, f2)
i1 = InStr(i + 1, Tmp, f4)

S(1) = Mid$(Tmp, i + Len(f2), i1 - i - Len(f2))

i = InStr(i1 + 1, Tmp, f3)
i1 = InStr(i + 1, Tmp, f4)

S(2) = Mid$(Tmp, i + Len(f3), i1 - i - Len(f3))

Open "out.txt" For Output As #1
For i = 0 To 2
    Print #1, S(i)
Next i
Close #1
End Sub
"Multitasking - ability to f##k up several things at once."
"It works better if you plug it in."
"The one who is digging the hole for the other to fall in is allready in it."

Kostko ::

Whoa, tale VB je pa skor tolk eleganten kot Python :D
Human stupidity is not convergent, it has no limit!

mHook ::

Ni problem v jeziku.

Tut v VBju se da narest enostavno:
Public Function GetVipek() As MatchCollection
    Dim url As String = "http://www.abancna-dzu.si/vipek.asp"
    Dim regex As String = "<div id=""[123]"" .*?nowrap>(.*?)</div>"
    Dim response As String = New WebClient().DownloadString(url)
    Return New Regex(regex).Matches(response)
End Function

StratOS ::

Hja, v VB6 :)
in ne v .net :)
"Multitasking - ability to f##k up several things at once."
"It works better if you plug it in."
"The one who is digging the hole for the other to fall in is allready in it."

darkolord ::

Čist isti šmorn, namesto webclienta uporabiš inet, regex pa uporabiš iz scripting runtime-a

David1994 ::

Najlepsa hvala vsem, se posebej mHooku in StratOSu. Na internetu sem konvertiral kodo ki mi jo je mHook napisal in izgleda nekako takole:

public MatchCollection GetVipek()
{

string url = "http://www.abancna-dzu.si/vipek.asp";

string regex = /* tega kosa nemorem pokazat ker mi skripta foruma na pusti */;

string response = new WebClient().DownloadString(url);

return new Regex(regex).Matches(response);

}

Lp, David:D :D :D

darkolord ::

Kodo lahko daš znotraj [ st.koda] in [ /st.koda] tagov (brez presledkov za uklepaji)

David1994 ::

Ok mam se en mali problemcek.

V double bi rad pritisnu eno decimalno stevilo. Pa mi zmeri nardi error "Input string was not in a correct format."
A mislite da je to bug v novem Visual C# "Orcas", ker je beta, zato vprasam... Koda pa izgleda nakako takole.

double stevilka;
string tekst = 0,07;

stevilka = double.Parse(tekst);


Meni ne izgleda nic narobe... vam?

darkolord ::

string tekst = "0,07";

David1994 ::

sam unos je ze pravi... ker ubistvu tale string skine od drugod... tko da tiste navednice res nimajo fore...
in ce bi bil problem bi mi zatezilo v vrstici kjer sem definiral tekst, ne v vrstici kjer se tekst konvertira... :(

jan01 ::

0.07, ne pa 0,07

David1994 ::

jan, najlepsa hvala, resil si mi par ur jajcanja po netu :D

Lp, David :D :D

darkolord ::

Meni v 2005 dela z obema varjantama

David1994 ::

Vem... saj lih to me je motlo :D
a ve kdo kako naj zatezim microsoftu?

darkolord ::

Na Connectu odpri bug report

mHook ::

double.Parse ti parsa tako kot je nastavljeno v System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.
V primeru culture sl-SI je privzeta vrednost za decimalke "," (vejica). "." (pika) v tem primeru kot ločilo za decimalke ne deluje - ga ignorira.
Če želiš drugače uporabi Double.Parse Method (String, IFormatProvider) ali pa v CurrentCulture.NumberFormat (ali kar celo CurrentCulture) od CurrentThread nastavi tisto, kar rabiš.

David1994 ::

Ok da nebom glih nove teme odpiral bom kar tle... iz borze na strani dela bi rad pobral vrednost krke... kako naj v vkodi definiram naj mi dobi dve vrstici nizje od tega?

<tr bgcolor="#F0F0F0" title="KRKA">
//// tle je vrstica ki me ne zanima
// ta je pa taprava


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[VB] Komunikacija s serijskimi napravami

Oddelek: Programiranje
412303 (1577) mNeRo
»

Excel vprašanje

Oddelek: Programiranje
81141 (1040) matic
»

Vb6

Oddelek: Programiranje
101327 (1190) StratOS
»

API+VB

Oddelek: Programiranje
262088 (1792) webblod
»

Odpiranje dat.exe v VB

Oddelek: Programiranje
122985 (2778) webblod

Več podobnih tem