3

Does anyone know how to fix it. I am using Mac OS 10.8.2

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/bs4/__init__.py", line 359
    print soup.prettify()
             ^
SyntaxError: invalid syntax
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Ian
  • 33
  • 1
  • 3
  • try running the import statement from the terminal, what do you get? – kennysong Oct 27 '12 at 03:29
  • 1
    How did you install bs4? Given that you apparently don't know what `pip` is, I'm wondering if you got it in some incorrect way… – abarnert Oct 27 '12 at 05:43
  • I installed it and can confirm that this problem does not exist in the latest version of BeautifulSoup, so you have by mistake installed an old Python 2 only version. – Lennart Regebro Oct 27 '12 at 08:00

3 Answers3

6

In Python 3 print is a function; it should be:

print(soup.prettify())

Install bs4 correctly or use a newer version if it is a bug. beautifulsoup4==4.1.3 works fine on Python 3.3.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I am surprised BeautifulSoup has this problem when it officially supports Python 3.x. Seems like monkey patch bs4 is the only way to go – K Z Oct 27 '12 at 03:39
  • Thanks, I am using 4.1.3. It should be a bug – Ian Oct 27 '12 at 04:10
  • @IanZhaoqiLeng: I've looked at installed 4.1.3 source in site-packages; it uses `()` correctly. I've installed it using `pip install beautifulsoup4`. – jfs Oct 27 '12 at 04:31
  • I used "pip install beautifulsoup4" But it shows "-bash: pip: command not found" – Ian Oct 27 '12 at 04:53
  • @J.F. Sebastian: Anyway, I switch to python 2.7, and everything just works. Thanks for your help! – Ian Oct 27 '12 at 04:59
  • 1
    You probably want `pip-3.3`, not `pip` (because the latter would install for 2.7). You may not have it yet, in which case you have to `easy_install-3.3 pip`. You may not even have `easy_install` yet for 3.3, in which case you have to manually download and install `distribute`. (For the stock Mac 2.7, you definitely have `easy_install` but not `pip`. Installing `pip` should be the first thing you do.) – abarnert Oct 27 '12 at 05:42
  • @abarnert: you should probably address your comment to Ian. I only use `pip` to install inside virtualenv. I should've mentioned it. Each virtualenv automatically installs distribute (easy_install), pip. It is the simplest way to install them. You shouldn't use easy_install unless it is necessary (if pip can't install a package) because there is [no uninstall](http://stackoverflow.com/q/1231688/4279) so you can't remove, update packages installed with easy_install properly. Also a rule of thumb: if you need `sudo` before pip than use system package manager instead to avoid breaking stuff. – jfs Oct 27 '12 at 06:05
  • My comment was addressed to Ian (comments on SO without a target always notify the OP), in particular his comment "-bash: pip: command not found". One of the cases where you need to use easy_install is to get pip itself, and that's the only thing I was recommending it for. Also, your rule of thumb only applies to linux; there *is* no package manager that can install stuff for the stock OS X Python, or for the python.org installer versions, so `sudo pip` (or `sudo pip-X.Y`) is the standard way to do things. – abarnert Oct 29 '12 at 21:45
  • @abarnert: comments on my *answer* without a target notify me, not the OP ([see p.1](http://meta.stackexchange.com/a/43020/137096)). There are package managers on OS X e.g., homebrew. – jfs Nov 02 '12 at 12:05
  • There are third-party add-on package managers, but there is no system package manager, and all of the add-on managers explicitly avoid touching system software. In particular, they will not install modules for the system Python, or for python.org GUI-installer versions. On top of that, Homebrew deliberately does not include Python (or Ruby or Perl) modules at all, explicitly recommending that you use pip for that. So your rule of thumb is still completely wrong on OS X. – abarnert Nov 02 '12 at 17:47
  • @abarnert: does `brew`-installed `pip` command requires `sudo`? – jfs May 07 '14 at 14:58
2

https://github.com/il-vladislav/BeautifulSoup4 Just copy this to Lib directory. It is fixed version for Python 3.3

JohnDow
  • 1,242
  • 4
  • 22
  • 40
0

Beautiful Soup 4 works on both Python 2 (2.6+) and Python 3.

Beautiful Soup is licensed under the MIT license, so you can also download the tarball, drop the bs4/ directory into almost any Python application (or into your library path) and start using it immediately. If you want to do this under Python 3, you will need to manually convert the code using 2to3.

http://www.crummy.com/software/BeautifulSoup/

use python ../python33/Tools/Script/2to3.py -w beautifulsoup4-4.3.2 is be ok

Coding Mash
  • 3,338
  • 5
  • 24
  • 45