Fórum Root.cz
Hlavní témata => Vývoj => Téma založeno: 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:
#!/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
-
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:
#!/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
-
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)
-
# -*- 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' ) )
-
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 ;)
-
velmi pekne dakujem.
requests bolo to co bolo potrebne....
este raz obom dakujem
-
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 ;)
-
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;)
-
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
-
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:
-
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 ;)