簡體   English   中英

無法在python 2.7和selenium中導入`beautifulSoup`

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM