与beautifulsoup4安装混淆

时间:2013-01-03 18:43:59

标签: python-2.7 beautifulsoup

我已经在我的windows7 PC中安装了python 2.7。现在我浏览了Beautiful Soup。现在我发现了两个安装BeautifulSoup4的命令。 easy_install beautifulsoup4pip install beautifulsoup4。但我的困惑在于我必须在哪个目录中运行这些命令。我的python文件夹是C:\Python27。你能帮我在哪里运行那个命令吗?

错误

    Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Arup Rakshit>cd..

C:\Users>cd..

C:\>cd C:\Python27

C:\Python27>python.exe virtualenv.py selenv
New python executable in selenv\Scripts\python.exe
Installing setuptools....................................done.
Installing pip.........................done.

C:\Python27>cd selenv\Scripts\

C:\Python27\selenv\Scripts>pip.exe install selenium
Downloading/unpacking selenium
  Downloading selenium-2.28.0.tar.gz (2.1MB): 2.1MB downloaded
  Running setup.py egg_info for package selenium
    C:\Python27\Lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'src_root'
      warnings.warn(msg)

    warning: no files found matching 'docs\api\py\index.rst'
Installing collected packages: selenium
  Running setup.py install for selenium
    C:\Python27\Lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'src_root'
      warnings.warn(msg)

    warning: no files found matching 'docs\api\py\index.rst'
Successfully installed selenium
Cleaning up...

C:\Python27\selenv\Scripts>python.exe my_selenium_script.py
python.exe: can't open file 'my_selenium_script.py': [Errno 2] No such file or d
irectory

C:\Python27\selenv\Scripts>python.exe my_selenium_script.py
Hello

C:\Python27\selenv\Scripts>cd..

C:\Python27\selenv>pip install beautifulsoup4
'pip' is not recognized as an internal or external command,
operable program or batch file.

C:\Python27\selenv>

由于

1 个答案:

答案 0 :(得分:1)

Beautiful Soup 4是通过PyPi发布的,因此如果您无法使用系统打包程序安装它,可以使用easy_install或pip进行安装。包名称是beautifulsoup4,相同的包适用于Python 2和Python 3。

$ easy_install beautifulsoup4

$ pip install beautifulsoup4

如果您没有安装easy_install或pip,可以下载Beautiful Soup 4源tarball并使用setup.py进行安装。 [转到该文件夹​​并运行命令setup.py install(假设路径扩展中已知* .py,否则运行c:\ python25 \ python setup.py install)。]

C:\Python27\beautifulsoup4-4.1.0>cd C:\Python27\beautifulsoup4-4.1.0
C:\Python27\beautifulsoup4-4.1.0>setup.py install 'here I am done!

测试:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Arup Rakshit>cd..

C:\Users>cd..

C:\>cd C:\Python27

C:\Python27>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib import urlopen
>>> from bs4 import BeautifulSoup
>>> source = BeautifulSoup(urlopen("http://www.google.com/"))
>>> tables = source.findAll('td')
>>> import csv
>>> writer = csv.writer(open('filename.csv','w'))
>>> writer.writerow(rows)