View Single Post
Old 01-09-2011, 12:51 AM   PM User | #1
MetalMichael
New Coder

 
Join Date: Aug 2010
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
MetalMichael is an unknown quantity at this point
Open url to perform operations

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);
preg_match('/regex\sand\sstuff/i',$page,$output)

Last edited by MetalMichael; 01-09-2011 at 12:52 AM.. Reason: adding 3.1 note
MetalMichael is offline   Reply With Quote