Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup HTMLParseError

New to Python, have a simple, situational question:

Trying to use BeautifulSoup to parse a series of pages.

from bs4 import BeautifulSoup
import urllib.request

BeautifulSoup(urllib.request.urlopen('http://bit.ly/'))

Traceback ...

html.parser.HTMLParseError: expected name token at '<!=KN\x01...

Working on Windows 7 64-bit with Python 3.2.

Do I need Mechanize? (which would entail Python 2.X)

like image 696
Zack Avatar asked Mar 23 '12 15:03

Zack


2 Answers

If that URL is correct, you're asking why an HTML parser throws an error parsing an MP3 file. I believe the answer to this to be self-evident...

like image 194
kindall Avatar answered Oct 27 '22 03:10

kindall


If you were trying to download that MP3, you could do something like this:

import urllib2

BLOCK_SIZE = 16 * 1024

req = urllib2.urlopen("http://bit.ly/xg7enD") 
#Make sure to write as a binary file
fp = open("someMP3.mp3", 'wb')
try:
  while True:
    data = req.read(BLOCK_SIZE)
    if not data: break
    fp.write(data)
finally:
  fp.close()
like image 32
ChicoBird Avatar answered Oct 27 '22 01:10

ChicoBird