[英]Couldn't import `beautifulSoup` in python 2.7 and 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
>>>
编辑
>>> 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
>
编辑1
>>> 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
>>>
更新
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
>>>
谢谢,
尝试大写BeautifulSoup
from BeautifulSoup import BeautifulSoup
如果您使用的是BS4:
from bs4 import BeautifulSoup
正如@poke聪明地建议的那样,确保已安装BeautifulSoup
是关键:)试试这个,它讨论了Windows上的BS安装: 如何在Windows上使用python 2.7安装 BeautifulSoup
Soup 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.