我已经在我的虚拟环境中安装了BeatifulSoup4,如下所示。我能够在我的虚拟环境解释器中正常导入和使用它;但是,当我直接运行脚本时,解释器无法找到BeatifulSoup。我还没有在我的原生环境中安装BeatifulSoup。如何强制我的脚本在安装了BeatifulSoup的虚拟环境中运行?
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')
这是我的" webscraping.py"的一小部分。代码以防万一。
(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
尝试在虚拟环境中运行但遇到TypeError。
(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">
在我的虚拟环境解释器中测试相同的代码行显示没有错误并按预期工作。
{{1}}
答案 0 :(得分:0)
您导出bs4
作为模块BeautifulSoup
,而不是您获得TypeError: 'module' object is not callable
的惊喜。模块BeautifulSoup
不可调用。
您必须访问模块中的某些内容(在您的案例中为BeautifulSoup
类)。有两种方法可以:访问类BeautifulSoup.BeautifulSoup
或直接导入:
from bs4 import BeautifulSoup