$ virtualenv test
$ source test/bin/activate
$ pip3 install beautifulsoup4
现在脚本 test.py
import urllib.request
import sys
import unittest, time, re
import requests
from bs4 import BeautifulSoup
class Sel(unittest.TestCase):
def setUp(self):
self.base_url = "file:///Users/ishandutta2007/Desktop/cars/cars-price-list.html"
def test_sel(self):
with urllib.request.urlopen(self.base_url) as url:
html_source = url.read()
data = html_source#.encode('utf-8')
parsed_html = BeautifulSoup(data, 'html.parser')
if __name__ == "__main__":
unittest.main()
当我运行 $python3 test.py
文件“test.py”,第 6 行,来自 bs4 import BeautifulSoup ModuleNotFoundError: No module named ‘bs4’
然后尝试
from beautifulsoup4 import BeautifulSoup
文件“test.py”,第 6 行,在 from beautifulsoup4 import BeautifulSoup ModuleNotFoundError: No module named ‘beautifulsoup4’
要求清楚地表明两者
$ pip3 freeze > requirements.txt
$ cat requirements.txt
beautifulsoup4==4.6.0
certifi==2018.1.18
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22
原文由 ishandutta2007 发布,翻译遵循 CC BY-SA 4.0 许可协议
只安装
BeautifulSoup4
而不是bs4
和BeautifulSoup
然后,做这个: