我对 Julia 还是很陌生,正在努力寻找一些小事来帮助我学习,但我目前陷入困境。我想使用 Selenium 在 Julia 中创建一个网络爬虫。WebDriver.jl
似乎没有更新为与 Julia 1.0+ 一起使用,并且我尝试使用 PyCall 受到错误的阻碍。
我的代码目前看起来像这样:
using PyCall
@pyimport selenium
@pyimport selenium.webdriver as webdriver
profile = webdriver.FirefoxProfile()
profile[:set_preference]("browser.download.folderList", 2)
profile[:set_preference]("browser.download.manager.showWhenStarting", false)
profile[:set_preference]("browser.download.dir", cd())
profile[:set_preference]("browser.helperApps.neverAsk.saveToDisk",
"text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
options = webdriver.FirefoxOptions()
options[:set_headless](headless=false)
driver = webdriver.Firefox(firefox_profile=profile, options=options, executable_path="/home/mcamp/PythonProjects/BudgetApp/geckodriver")
最后一行给出了这个错误:
ERROR: PyError ($(Expr(:escape, :(ccall(#= /home/mcamp/.julia/packages/PyCall/0jMpb/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'selenium.common.exceptions.SessionNotCreatedException'>
SessionNotCreatedException('Failed to set preferences: Unable to read profile preferences file', None, None)
File "/home/mcamp/anaconda3/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/home/mcamp/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/mcamp/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/mcamp/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/mcamp/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
Stacktrace:
[1] pyerr_check at /home/mcamp/.julia/packages/PyCall/0jMpb/src/exception.jl:60 [inlined]
[2] pyerr_check at /home/mcamp/.julia/packages/PyCall/0jMpb/src/exception.jl:64 [inlined]
[3] macro expansion at /home/mcamp/.julia/packages/PyCall/0jMpb/src/exception.jl:84 [inlined]
[4] __pycall!(::PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyObject, ::PyObject) at /home/mcamp/.julia/packages/PyCall/0jMpb/src/pyfncall.jl:44
[5] _pycall!(::PyObject, ::PyObject, ::Tuple{}, ::Int64, ::PyObject) at /home/mcamp/.julia/packages/PyCall/0jMpb/src/pyfncall.jl:22
[6] _pycall!(::PyObject, ::PyObject, ::Tuple{}, ::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol,Symbol},NamedTuple{(:firefox_profile, :options, :executable_path),Tuple{PyObject,Nothing,String}}}) at /home/mcamp/.julia/packages/PyCall/0jMpb/src/pyfncall.jl:11
[7] #call#89 at /home/mcamp/.julia/packages/PyCall/0jMpb/src/pyfncall.jl:89 [inlined]
[8] (::getfield(PyCall, Symbol("#kw#PyObject")))(::NamedTuple{(:firefox_profile, :options, :executable_path),Tuple{PyObject,Nothing,String}}, ::PyObject) at ./none:0
[9] top-level scope at none:0
firefox_profile
通过排除和 ,我可以让 Firefox 启动并做一些简单的事情options
。我认为正在发生的事情是,我正在尝试将 Python 对象传递给该方法,而 Julia 可能围绕它进行了变异,因此 Python 不确定如何处理它。
TIA