urlencoding values in urllib

This is the place for queries that don't fit in any of the other categories.

urlencoding values in urllib

Postby metulburr » Thu Jun 27, 2013 2:49 pm

I must of forgot a step, but it doesnt look it when i compare it to examples. I am trying to input a value into the search field. But the webpage opens with the default value instead of the one i gave. What did i forget?

python3.x
Code: Select all
from urllib.request import urlopen, Request
from urllib.parse import urlencode

def open_in_browser(doc):
    '''testing'''
    import webbrowser as WB
    f = open('test.html', 'w')
    f.write(doc)
    f.close()
    WB.open('test.html')

url = 'http://www.webmd.com/diet/healthtool-food-calorie-counter'

values = {
 'value':'pear'
}

data = urlencode(values).encode()
req = Request(url, data)
res = urlopen(req)
html = res.read().decode()

open_in_browser(html)
print(html)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1385
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 1 guest