我有一个.py文件,该文件过去曾用于使用硒和phantomjs从gasbuddy中刮取汽油价格。它曾经工作过,但突然停了下来。因此,我卸载并重新安装了selenium和phantomJS。我使用自制软件卸载并重新安装了Python 3。我什至删除了MacBook并重新安装了Sierra,然后重新安装了其他所有东西。对我来说,没有意义的是我导入了甚至没有导入的模块的错误。
import os
from selenium import webdriver
import warnings
warnings.filterwarnings("ignore")
driver = webdriver.PhantomJS(service_log_path=os.path.devnull)
driver.get('https://www.gasbuddy.com/station/155967')
price = driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div/h1')
print(price.text)
和我得到的错误是:
Traceback (most recent call last):
File "/Users/igisan/Desktop/scripts/gas.py", line 2, in <module>
from selenium import webdriver
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 29, in <module>
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 27, in <module>
from .remote_connection import RemoteConnection
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 24, in <module>
import urllib3
File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
from .exceptions import (
File "/usr/local/lib/python3.7/site-packages/urllib3/exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import (
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 71, in <module>
import email.parser
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/feedparser.py", line 27, in <module>
from email._policybase import compat32
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/utils.py", line 33, in <module>
from email._parseaddr import quote
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/_parseaddr.py", line 16, in <module>
import time, calendar
File "/Users/igisan/Desktop/scripts/calendar.py", line 2, in <module>
from pyicloud import PyiCloudService
File "/usr/local/lib/python3.7/site-packages/pyicloud/__init__.py", line 2, in <module>
from pyicloud.base import PyiCloudService
File "/usr/local/lib/python3.7/site-packages/pyicloud/base.py", line 7, in <module>
import requests
File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 46, in <module>
from .exceptions import RequestsDependencyWarning
File "/usr/local/lib/python3.7/site-packages/requests/exceptions.py", line 9, in <module>
from urllib3.exceptions import HTTPError as BaseHTTPError
ImportError: cannot import name 'HTTPError' from 'urllib3.exceptions' (/usr/local/lib/python3.7/site-packages/urllib3/exceptions.py)
我什至更新了所有内容。安装和重新安装urllib并没有什么不同。我也尝试过使用其他网络抓取方法(lxml等),但无济于事。有答案吗?
答案 0 :(得分:2)
tl;博士
您有urllib3.exceptions
的循环导入。 /Users/igisan/Desktop/scripts/calendar.py
遮盖了Python标准库的calendar
模块。重命名该模块。
您的Python环境似乎有点混乱。导入webdriver
看起来很正常,直到urllib3.exceptions
的基础导入将http.client
解析为不是本地Python 3.7解释器的站点包,而是解析为似乎是自制的站点包在/usr/local/Cellar
下安装Python 3.7.2。
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 71, in <module>
import email.parser
从那里开始,在该Homebrew模块中有一条正常的导入链,直到email._parseaddr
尝试导入calendar
为止。
此导入应转到Python标准库中的calendar.py
模块,但由于您的calendar.py
文件夹中有一个/Users/igisan/Desktop/scripts
,显然已添加到{{1} }(大概通过sys.path
),解释器解析为您的 PYTHONPATH
。
calendar.py
您的 File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/_parseaddr.py", line 16, in <module>
import time, calendar
File "/Users/igisan/Desktop/scripts/calendar.py", line 2, in <module>
from pyicloud import PyiCloudService
从calendar.py
包中导入某些类,这将导致尝试导入pyicloud
,这将导致尝试导入{{ 1}}。
这将在此时成为循环导入。导入requests
导致尝试导入urllib3.exceptions
的模块的导入。
应该通过将urllib3.exceptions
重命名为不会遮盖来自标准库的模块urllib3.exceptions
的东西来解决此问题。
答案 1 :(得分:0)
将主模块重命名为email.py
之后,我遇到了同样的问题。它开始与库requests
和selenium
发生冲突。从email.py
重命名为更原始的内容后,错误消失了。