[英]Unable to run BeautifulSoup in Python Virtual Environment
I've installed BeatifulSoup4 in my virtual environment as shown below. 我已经在虚拟环境中安装了BeatifulSoup4,如下所示。 I'm able to import and use it normally inside my virtual environment interpreter;
我可以在虚拟环境解释器中正常导入和使用它; however, the interpreter couldn't find BeatifulSoup when I run the script directly.
但是,当我直接运行脚本时,解释器找不到BeatifulSoup。 I haven't installed BeatifulSoup in my native environment.
我尚未在本机环境中安装BeatifulSoup。 How do I force my script to run in a virtual environment where BeatifulSoup is installed?
如何强制我的脚本在安装BeatifulSoup的虚拟环境中运行?
(virtual) $ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
beautifulsoup4 (4.6.0)
bs4 (0.0.1)
certifi (2017.11.5)
cffi (1.9.1)
chardet (3.0.4)
colored (1.3.4)
cryptography (1.6)
et-xmlfile (1.0.1)
idna (2.6)
jdcal (1.3)
numpy (1.13.1)
openpyxl (2.4.8)
pandas (0.20.3)
paramiko (2.0.2)
pip (9.0.1)
progressbar2 (3.34.2)
psycopg2 (2.6.2)
pyasn1 (0.1.9)
pycparser (2.17)
python-dateutil (2.6.1)
python-utils (2.2.0)
pytz (2017.2)
readline (6.2.4.1)
requests (2.18.4)
setuptools (28.8.0)
six (1.10.0)
terminaltables (3.1.0)
urllib3 (1.22)
wheel (0.29.0)
xlrd (1.1.0)
(virtual) $
(virtual) $ python --version
Python 3.5.2
Here's a snippet of my "webscraping.py" code in case it helps. 如果有帮助,这是我的“ webscraping.py”代码的一部分。
import bs4 as BeautifulSoup
from requests import get
url = 'http://www.imdb.com/search/title?release_date=2017&sort=num_votes,desc&page=1'
response = get (url)
print ('Result from response:')
print (response.text [:500])
html = BeautifulSoup (response.text, 'html.parser')
Attempting to run in a virtual environment but got a TypeError. 尝试在虚拟环境中运行,但出现TypeError。
(virtual) $ python webscraping.py
Result from response:
<!DOCTYPE html>
<html
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-itunes-app" content="app-id=342792525, app-argument=imdb:///?src=mdot">
<script type="text/javascript">var ue_t0=window.ue_t0||+new Date();</script>
<script type="text/javascript">
var ue_mid = "A1EVAM02EL8SFB";
var
Traceback (most recent call last):
File "webscraping.py", line 88, in <module>
main ()
File "webscraping.py", line 39, in main
html = BeautifulSoup (response.text, 'html.parser')
TypeError: 'module' object is not callable
Testing the same line of code in my virtual environment interpreter show no error and work as expected. 在我的虚拟环境解释器中测试同一行代码不会显示任何错误,并且可以按预期工作。
(virtual) $ python
Python 3.5.2 (default, Nov 22 2016, 19:03:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from bs4 import BeautifulSoup
>>> from requests import get
>>>
>>> url = 'http://www.imdb.com/search/title?release_date=2017&sort=num_votes,desc&page=1'
>>> response = get (url)
>>> print (response.text [:500])
<!DOCTYPE html>
<html
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-itunes-app" content="app-id=342792525, app-argument=imdb:///?src=mdot">
<script type="text/javascript">var ue_t0=window.ue_t0||+new Date();</script>
<script type="text/javascript">
var ue_mid = "A1EVAM02EL8SFB";
var
>>>
>>> html = BeautifulSoup (response.text, 'html.parser')
>>> html
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://ogp.me/ns#">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="app-id=342792525, app-argument=imdb:///?src=mdot" name="apple-itunes-app"/>
<script type="text/javascript">var ue_t0=window.ue_t0||+new Date();</script>
<script type="text/javascript">
You imported bs4
as a module BeautifulSoup
, not a surprize you got TypeError: 'module' object is not callable
. 您将
bs4
导入为模块BeautifulSoup
,这并不bs4
,您收到TypeError: 'module' object is not callable
。 Module BeautifulSoup
is not callable. 模块
BeautifulSoup
不可调用。
You have to access something in the module (class BeautifulSoup
in your case). 您必须访问模块中的某些内容(本例中为
BeautifulSoup
类)。 There're 2 ways to do that: either access class BeautifulSoup.BeautifulSoup
or import it directly: 有两种方法可以执行此操作:访问类
BeautifulSoup.BeautifulSoup
或直接将其导入:
from bs4 import BeautifulSoup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.