2012-12-26 58 views
0

我正在嘗試導入beautifulSoup但出現錯誤。請告訴我爲什麼這麼做或指導我解決這個問題?無法在python 2.7和selenium中導入`beautifulSoup`

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

C:\Users\Arup Rakshit>python 
'python' is not recognized as an internal or external command, 
operable program or batch file. 

C:\Users\Arup Rakshit>ipython 
'ipython' is not recognized as an internal or external command, 
operable program or batch file. 

C:\Users\Arup Rakshit>cd.. 

C:\Users>cd.. 

C:\>cd Python27 

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

C:\Python27\selenv\Scripts>my_selenium_script.py 
hello 

C:\Python27\selenv\Scripts>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 beautifulSoup import BeautifulSoup 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named beautifulSoup 
>>> 

EDIT

>>> from BeautifulSoup import BeautifulSoup 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named BeautifulSoup 
>>> 

from bs4 import BeautifulSoup 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named bs4 

>

EDIT1

>>> from urllib import urlopen 
>>> from bs4 import BeautifulSoup 
>>> source = BeautifulSoup(urlopen(url)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'url' is not defined 
>>> source = BeautifulSoup(urlopen(https://demo.aravo.com)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(https://demo.aravo.com)) 
             ^
SyntaxError: invalid syntax 
>>> from urllib import urlopen 
>>> from bs4 import BeautifulSoup 
>>> source = BeautifulSoup(urlopen(https://demo.aravo.com/)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(https://demo.aravo.com/)) 
             ^
SyntaxError: invalid syntax 
>>> source = BeautifulSoup(urlopen(demo.aravo.com/)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(demo.aravo.com/)) 
               ^
SyntaxError: invalid syntax 
>>> source = BeautifulSoup(urlopen(demo.aravo.com)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'demo' is not defined 
>>> 

UPDATE

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

C:\Python27\selenv\Scripts>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("https://demo.aravo.com/")) 
>>> tables = source.findAll('td') 
>>> import csv 
>>> writer = csv.writer(open('filename.csv','w')) 
>>> writer.writerow(rows) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'rows' is not defined 
>>> 

感謝,

+0

嗯...它看起來像Python尚未添加到PATH變量一開始......'my_selinuim_script.py'中發生了什麼? – Ben

+0

@Ben'my_selinuim_script.py'給我輸出'hello'。我認爲它可以! – CodeLover

回答

7

嘗試資本BeautifulSoup

from BeautifulSoup import BeautifulSoup 

如果您正在使用BS4:

from bs4 import BeautifulSoup 

由@poke智能提示,確保您安裝了BeautifulSoup是關鍵:)嘗試此操作,討論Windows上的BS安裝:How to install beautiful soup 4 with python 2.7 on windows

+3

如果兩者都不起作用,則需要首先[安裝](http://www.crummy.com/software/BeautifulSoup/#Download)。 – poke

+0

請參閱編輯仍然**錯誤** – CodeLover

+0

@ user1897085然後,您需要安裝美麗的湯... – poke