Fórum Root.cz

Hlavní témata => Vývoj => Téma založeno: alans 09. 11. 2013, 23:41:30

Název: Python a získání informací s webu
Přispěvatel: alans 09. 11. 2013, 23:41:30
Zdravim vsetkych.... snazim sa ziskat zakladne informacie z webu www.zrsr.sk.

Zistil som, ze na to aby sa dalo vyhladavat treba mat zapate cookies, takze logicky som sa znazil cookie odchytit  poslat... moj pokus vyzera takto:
Kód: [Vybrat]
#!/usr/bin/python
# -*- coding: utf-8 -*-
from urllib2 import Request
urlx= "http://www.zrsr.sk/zr_ico.aspx"
request = Request(url="http://www.zrsr.sk/zr_browse.aspx")
ico = '36379395'

import urllib2
import urllib
from cookielib import CookieJar

cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# input-type values from the html form
formdata = {"tico":ico, "cmdVyhladat":"submit", }
data_encoded = urllib.urlencode(formdata)
response = opener.open("http://www.zrsr.sk/zr_ico.aspx", data_encoded)
content = response.read()
cj.add_cookie_header(request)
response = opener.open(request, data_encoded)
print response.read()

Problem je ten ze v kazdom pripadae dostavam prazdne vyhladavanie... riesim to uz asi 3hodiny a nie a nie na to prist.

Dakujem za kazdu pomoc

Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: xxar3s 10. 11. 2013, 00:33:46
Zdravim vsetkych.... snazim sa ziskat zakladne informacie z webu www.zrsr.sk.

Zistil som, ze na to aby sa dalo vyhladavat treba mat zapate cookies, takze logicky som sa znazil cookie odchytit  poslat... moj pokus vyzera takto:
Kód: [Vybrat]
#!/usr/bin/python
# -*- coding: utf-8 -*-
from urllib2 import Request
urlx= "http://www.zrsr.sk/zr_ico.aspx"
request = Request(url="http://www.zrsr.sk/zr_browse.aspx")
ico = '36379395'

import urllib2
import urllib
from cookielib import CookieJar

cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# input-type values from the html form
formdata = {"tico":ico, "cmdVyhladat":"submit", }
data_encoded = urllib.urlencode(formdata)
response = opener.open("http://www.zrsr.sk/zr_ico.aspx", data_encoded)
content = response.read()
cj.add_cookie_header(request)
response = opener.open(request, data_encoded)
print response.read()

Problem je ten ze v kazdom pripadae dostavam prazdne vyhladavanie... riesim to uz asi 3hodiny a nie a nie na to prist.

Dakujem za kazdu pomoc

Robis to zle bo sa tam pouziva viewstate a na ten musis mat zapnuty aj JS
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pulpe 10. 11. 2013, 09:19:06
Kód: [Vybrat]
import requests
import re

r = requests.session()
page = r.get('http://www.zrsr.sk/zr_ico.aspx')

hidden = dict(re.findall('<input type="hidden" name=".*" id="(.*)" value="(.*)" />', page.text))
hidden['__EVENTTARGET'] = ''
hidden['__EVENTARGUMENT'] = ''
hidden['cmdVyhladat'] = 'Vyhľadať'
hidden['msg1'] = 'IČO musí tvoriť 8 číslic!'
hidden['tico'] = '36379395'

page = r.post('http://www.zrsr.sk/zr_ico.aspx', data=hidden)
print(page.text)
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pecko 10. 11. 2013, 18:41:48
# -*- coding: utf-8 -*-

import requests
import re

r = requests.session()
page = r.get( 'http://www.zrsr.sk/zr_ico.aspx (http://www.zrsr.sk/zr_ico.aspx)' )

hidden = dict( re.findall( '<input type="hidden" name=".*" id="(.*)" value="(.*)" />', page.content ) )

hidden['__EVENTTARGET'] = ''
hidden['__EVENTARGUMENT'] = ''
hidden['cmdVyhladat'] = 'Vyhľadať'
hidden['msg1'] = 'IČO musí tvoriť 8 číslic!'
hidden['tico'] = '36379395'

page = r.post( 'http://www.zrsr.sk/zr_ico.aspx (http://www.zrsr.sk/zr_ico.aspx)', data = hidden )

if 302 == page.status_code and '' != page.headers['location']:
    page = r.get( 'http://www.zrsr.sk (http://www.zrsr.sk)' + page.headers['location'] )

print( page.content.encode( 'utf-8' ) )
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pulpe 10. 11. 2013, 20:05:54
Kód: [Vybrat]
if 302 == page.status_code and '' != page.headers['location']:
    page = r.get( '[url=http://www.zrsr.sk]http://www.zrsr.sk[/url]' + page.headers['location'] )

Defaultně má requests nastaveno 'allow_redirects=True', takže toto není potřeba  ;)
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: alans 10. 11. 2013, 20:14:47
velmi pekne dakujem.
requests bolo to co bolo potrebne....

este raz obom dakujem
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pecko 10. 11. 2013, 20:34:44
Kód: [Vybrat]
if 302 == page.status_code and '' != page.headers['location']:
    page = r.get( '[url=http://www.zrsr.sk]http://www.zrsr.sk[/url]' + page.headers['location'] )

skusal som, nefungovalo. preto som to tam pridal. ale mozno som len nieco zle robil. nevadi, hlavne ze mu to ide a je spokojny;)

Defaultně má requests nastaveno 'allow_redirects=True', takže toto není potřeba  ;)
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pecko 11. 11. 2013, 09:14:26
Kód: [Vybrat]
if 302 == page.status_code and '' != page.headers['location']:
    page = r.get( '[url=http://www.zrsr.sk]http://www.zrsr.sk[/url]' + page.headers['location'] )

Defaultně má requests nastaveno 'allow_redirects=True', takže toto není potřeba  ;)

takze toto tak celkom pravda nebude. True to ma len get, nie post. ked som to manualne nastavil na True pre post, hlasilo to nejaku html chybu. ked som to manualne nastavil na False, fungovalo to aj s tou mojou kontrolou. bez nej nie. tak neviem teda;)
Název: Re:[Python] Ziskanie informacii z webu www.zrsr.sk
Přispěvatel: pulpe 11. 11. 2013, 23:36:28
takze toto tak celkom pravda nebude. True to ma len get, nie post. ked som to manualne nastavil na True pre post, hlasilo to nejaku html chybu. ked som to manualne nastavil na False, fungovalo to aj s tou mojou kontrolou. bez nej nie. tak neviem teda;)

Mě to funguje, tak jak jsem to napsal.  :)

btw. Python 3 a Requests 2.0.1
Název: Re:Python a získání informací s webu
Přispěvatel: pulpe 11. 11. 2013, 23:39:23
Citace: http://www.python-requests.org/en/latest/user/quickstart/#redirection-and-history
Requests will automatically perform location redirection for all verbs except HEAD.
.....
If you’re using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable redirection handling with the allow_redirects parameter:
Název: Re:Python a získání informací s webu
Přispěvatel: pecko 12. 11. 2013, 20:23:30
Citace: http://www.python-requests.org/en/latest/user/quickstart/#redirection-and-history
Requests will automatically perform location redirection for all verbs except HEAD.
.....
If you’re using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable redirection handling with the allow_redirects parameter:

hmm, v 3 mozno. ja mam 2.7 a tam maju requests globalne False pre allow_redirects, len pre get je nastavene na True. post ma ale default False ;)