2013-07-07 13 views
17

Ho visto le domande come questo ha chiesto molte volte ma nessuno è utileInvio a un modulo web utilizzando python

Nel tentativo di inviare dati a un modulo a ive web richieste provato, e urllib e nessuno hanno lavorato

per esempio qui è il codice che deve cercare il tag [python] su SO:

import urllib 
import urllib2 

url = 'http://stackoverflow.com/' 

# Prepare the data 
values = {'q' : '[python]'} 
data = urllib.urlencode(values) 

# Send HTTP POST request 
req = urllib2.Request(url, data) 
response = urllib2.urlopen(req) 

html = response.read() 

# Print the result 
print html 

ancora quando l'eseguo ottengo il soure html della home page

Ecco un esempio di utilizzo di richieste:

import requests 

data= { 
    'q': '[python]' 
    } 
r = requests.get('http://stackoverflow.com', data=data) 

print r.text 

stesso risultato! Non capisco perché questi metodi non funzionino li ho provati su vari siti senza successo quindi se qualcuno ha fatto con successo questo per favore mostrami come!

Grazie mille!

+0

Sembra un lavoro per [tag: mechanize-python]! – Johnsyweb

+0

sì, guardatelo, ma mi sto ancora chiedendo perché non funzioneranno !? – Serial

risposta

19

Se si desidera passare q come parametro nella URL utilizzando requests, utilizzare l'argomento params, non data (vedi Passing Parameters In URLs):

r = requests.get('http://stackoverflow.com', params=data) 

Questo chiederà https://stackoverflow.com/?q=%5Bpython%5D, che non è quello che sei cercando.

si vuole veramente POST ad un modulo. Prova questo:

r = requests.post('https://stackoverflow.com/search', data=data) 

Questo è essenzialmente lo stesso di GET -ting https://stackoverflow.com/questions/tagged/python, ma credo che si otterrà l'idea da questo.

+2

wow che funzionava davvero ho provato un sacco bu non sapevo perché non potevo capirlo grazie! – Serial

+1

ha funzionato per me. Grazie – MegaBytes

9
import urllib 
import urllib2 

url = 'http://www.someserver.com/cgi-bin/register.cgi' 
values = {'name' : 'Michael Foord', 
     'location' : 'Northampton', 
     'language' : 'Python' } 

data = urllib.urlencode(values) 
req = urllib2.Request(url, data) 
response = urllib2.urlopen(req) 
the_page = response.read() 

Questo rende una richiesta POST con i dati specificati nei valori. abbiamo bisogno di urllib per codificare l'url e quindi urllib2 per inviare una richiesta.

+0

Questo è esattamente lo stesso di quello che l'op ha ... – KindaTechy

0

La libreria Mechanize di Python è anche perfetta per consentire di inviare moduli. È possibile utilizzare il seguente codice per creare un oggetto browser e creare richieste.

import mechanize,re 
br = mechanize.Browser() 
br.set_handle_robots(False) # ignore robots 
br.set_handle_refresh(False) # can sometimes hang without this 
br.addheaders = [('User-agent', 'Firefox')]    
br.open("http://google.com") 
br.select_form('f') 
br.form[ 'q' ] = 'foo' 
br.submit() 
resp = None 

for link in br.links(): 
    siteMatch = re.compile('www.foofighters.com').search(link.url) 

    if siteMatch: 
     resp = br.follow_link(link) 
     break 

content = resp.get_data() 
print content 
Problemi correlati