View Full Version : Open url to perform operations

01-09-2011, 01:51 AM
Okay, well I'm pretty new to python. I can open a url, but when it comes to doing anything with it, I keep getting errors. Pretty sure I'm doing it wrong.

So I've imported urllib.request, and do:
youtubesearch = urllib.request.urlopen('http://www.youtube.com/results?search_query=' + query)

this opening works, but then when I want to do something with it, for example, perform regex operations, I the error TypeError: expected string or buffer.
code used:
youtubere = re.compile('watch\?v=([^\"&]+)"\stitle[\s\S]{0,400}?<b>' + query, re.I)
youtubesearch = urllib.request.urlopen('http://www.youtube.com/results?search_query=' + query)
youtubelink = re.search(youtubere, youtubesearch)
Full error:
(other traces)
File "C:\Python31\lib\re.py", line 157, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

Edit: Note, using python 3.1

Just for note... this is the sort of thing that if I were using php, I would do
$page = file_get_contents($Url);

01-09-2011, 12:44 PM
urllib.request.urlopen returns a file like object so you need to use the read method of a file like object:

youtubelink = re.search(youtubere, youtubesearch.read())

01-09-2011, 05:33 PM
Ah, thanks, still thinking in a non python mind-set...

However, when I try this, I get the error:
TypeError: can't use a string pattern on a bytes-like object

edit: thanks, works when I use

youtubelink = re.search(youtubere, youtubesearch.read().decode('utf-8')