我正在尝试构建一个实用程序函数来向浏览器输出漂亮的汤代码我有以下代码:
def bs4_to_browser(data):
from selenium import webdriver
driver = webdriver.Firefox(path="F:\FirefoxPortable\Firefox.exe")
driver.get("about:blank")
# data = '<h1>test</h1>' # supposed to come from BeautifulSoup -- this works when uncommented
driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))
return
>>> listing
<div class="listingHeader">
....
>>> type(listing)
<class 'bs4.element.Tag'>
>>> a = unicode(listing)
>>> type(a)
<type 'unicode'>
现在如果我跑:
>>> bs4_to_browser(a)
Traceback (most recent call last):
File "F:\PyCharm 3.4.1\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "F:/envs/r1/masslist\ml1\views.py", line 606, in bs4_to_browser
driver = webdriver.Firefox(firefox_binary=binary)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 59, in __init__
self.binary, timeout),
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 61, in launch_browser
self._wait_until_connectable()
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 100, in _wait_until_connectable
self._get_firefox_output())
WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: '
>>> bs4_to_browser(a)
Traceback (most recent call last):
File "F:\PyCharm 3.4.1\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "F:/envs/r1/masslist\ml1\views.py", line 610, in bs4_to_browser
driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 401, in execute_script
{'script': script, 'args':converted_args})['value']
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'missing ; before statement' ; Stacktrace:
at handleEvaluateEvent (about:blank:68:11)
我该如何解决这个问题?
附录:
关于评论:
1)WebDriverException:消息:u'syntax error'; Stacktrace:由linechange生成。
2)
>>> a = unicode(listing)
>>> a = a.replace('\n', '')
>>> bs4_to_browser(a)
Traceback (most recent call last):
File "F:\PyCharm 3.4.1\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "F:/envs/r1/masslist\ml1\views.py", line 610, in bs4_to_browser
driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 401, in execute_script
{'script': script, 'args':converted_args})['value']
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'missing ; before statement' ; Stacktrace:
at handleEvaluateEvent (about:blank:68:11)
>>> a = str(listing)
>>> a = a.replace('\n', '')
>>> bs4_to_browser(a)
Traceback (most recent call last):
File "F:\PyCharm 3.4.1\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "F:/envs/r1/masslist\ml1\views.py", line 606, in bs4_to_browser
driver = webdriver.Firefox(firefox_binary=binary)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 59, in __init__
self.binary, timeout),
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 61, in launch_browser
self._wait_until_connectable()
File "F:\envs\r1\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 100, in _wait_until_connectable
self._get_firefox_output())
WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: '
>>> bs4_to_browser(a)
Traceback (most recent call last):
File "F:\PyCharm 3.4.1\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "F:/envs/r1/masslist\ml1\views.py", line 610, in bs4_to_browser
driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 401, in execute_script
{'script': script, 'args':converted_args})['value']
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "F:\envs\r1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'missing ; before statement' ; Stacktrace:
at handleEvaluateEvent (about:blank:68:11)
3)我正在使用FF 31.1 ESR portable和selenium 2.42.1
我还注意到,如果在运行bs4_to_browser之前浏览器没有完全关闭,你会得到:“WebDriverException:消息:'浏览器似乎已经退出,然后才能连接。输出为:'”