【发布时间】:2016-02-26 05:09:38
【问题描述】:
这里有一个奇怪的问题。我有一个 24/7 运行的 Python 3 脚本,并使用 Selenium 和 Firefox 访问网页,每 5 分钟从下载链接下载一个文件(我不能只使用 urllib 或其他方式下载该文件,因为即使下载文件的链接地址保持不变,文件中的数据不断变化,每次重新加载页面时都会有所不同,也取决于指定的标准)。该脚本几乎一直运行良好,但我无法摆脱这个偶尔弹出的错误,该错误会终止脚本。这是错误:
Traceback (most recent call last):
File "/Users/Shared/ROTH_1/Folio/get_F_notes.py", line 248, in <module>
driver.get(search_url)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 187, in get
self.execute(Command.GET, {'url': url})
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
response = self.command_executor.execute(driver_command, params)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
return self._request(command_info[0], url, body=data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 379, in _request
self._conn.request(method, parsed_url.path, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1118, in _send_request
self.putrequest(method, url, **skips)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 966, in putrequest
raise CannotSendRequest(self.__state)
http.client.CannotSendRequest: Request-sent
这是我的脚本中出现问题的部分,具体来说,脚本命中了“除了 ConnectionRefusedError:”部分,并按预期打印出“警告 1:ConnectionRefusedError:搜索页面未加载;现在正在尝试再次”。但是,我认为,当循环再次开始并再次尝试“driver.get(search_url)”时,我得到了上述错误。脚本在这一点上窒息并给了我上述错误。
我对此进行了相当多的研究,似乎该脚本试图重用第一次尝试时的相同连接。修复似乎是创建一个新的连接。但这就是我所能收集到的全部信息,而且我不知道如何与 Selenium 建立新的联系。你?或者,这里还有其他问题吗?
search_url = 'https://www.example.com/download_page'
loop_get_search_page = 1
while loop_get_search_page < 7:
if loop_get_search_page == 6:
print('WARNING: tried to load search page 5 times; exiting script to try again later')
##### log out
try:
driver.find_element_by_link_text('Sign Out')
except NoSuchElementException:
print('WARNING: NoSuchElementException: Unable to find the link text for the "Sign Out" button')
driver.quit()
raise SystemExit
try:
driver.get(search_url)
except TimeoutException:
print('WARNING ', loop_get_search_page, ': TimeoutException: search page did not load; now trying again', sep='')
loop_get_search_page += 1
continue
except ConnectionRefusedError:
print('WARNING ', loop_get_search_page, ': ConnectionRefusedError: search page did not load; now trying again')
loop_get_search_page += 1
continue
else:
break
【问题讨论】: