Python a získání informací s webu

alans

Python a získání informací s webu
« kdy: 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

« Poslední změna: 11. 11. 2013, 10:04:57 od Petr Krčmář »


Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #1 kdy: 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

pulpe

Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #2 kdy: 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)

pecko

  • ***
  • 105
    • Zobrazit profil
    • E-mail
Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #3 kdy: 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' )

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', data = hidden )

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

print( page.content.encode( 'utf-8' ) )

pulpe

Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #4 kdy: 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  ;)


alans

Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #5 kdy: 10. 11. 2013, 20:14:47 »
velmi pekne dakujem.
requests bolo to co bolo potrebne....

este raz obom dakujem

pecko

  • ***
  • 105
    • Zobrazit profil
    • E-mail
Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #6 kdy: 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  ;)

pecko

  • ***
  • 105
    • Zobrazit profil
    • E-mail
Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #7 kdy: 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;)

pulpe

Re:[Python] Ziskanie informacii z webu www.zrsr.sk
« Odpověď #8 kdy: 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

pulpe

Re:Python a získání informací s webu
« Odpověď #9 kdy: 11. 11. 2013, 23:39:23 »
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:

pecko

  • ***
  • 105
    • Zobrazit profil
    • E-mail
Re:Python a získání informací s webu
« Odpověď #10 kdy: 12. 11. 2013, 20:23:30 »
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 ;)