我試圖制作一個機器人,它可以查詢 Ajax Amsterdamn 的 Bob Marley TShirt 是否可用。
https://www.adidas.ch/de/ajax-3-jsy/GT9559.html <- 有鏈接
我能夠讓它運行(還添加了一個電報機器人來報告成功:
#!/usr/bin/python3
from selenium import webdriver
import requests
token="" # expunged for obvious reasons
chat="" # expunged for obvious reasons
message="Available"
fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
browser = webdriver.Firefox(firefox_options=fireFoxOptions)
browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
send_text = 'https://api.telegram.org/bot' token '/sendMessage?chat_id=' chat '&parse_mode=Markdown&text=' message
response = requests.get(send_text)
print(response.json())
browser.close()
致力于:
- 硒 3.141.0
- 蟒蛇 3
- 火狐 91.0.2
- 壁虎驅動程式 0.29.1
- 作業系統:Manjaro Linux
所以在那之后,我嘗試將它部署在我的 Debian 10 服務器上,但這就是斗爭開始的地方。我必須安裝 Firefox 78.14.0esr 并根據 Geckodriver 0.27.0 版的 github 發布頁面。Selenium 與 3.141.0 保持不變。據我所知和我研究的版本應該沒問題,但是當執行時拋出這個令人傷腦筋的錯誤:
Traceback (most recent call last):
File "./ajax.py", line 18, in <module>
browser = webdriver.Firefox(options=options) #, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
我搜索了錯誤,顯然你必須定義二進制路徑和“牽線木偶”功能,現在我這樣做了,現在代碼看起來像這樣(包括一些除錯內容):
#!/usr/bin/python3
from selenium import webdriver
import requests
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#token="1995953311:AAH-D7S-MISkCa0yQxUc84Gf978fz0vtoqY"
#chat="1917512203"
message="just a test"
options = FirefoxOptions()
options.add_argument("--headless")
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
binary = "/usr/bin/firefox"
options.binary = binary
browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
send_text = 'https://api.telegram.org/bot' token '/sendMessage?chat_id=' chat '&parse_mode=Markdown&text=' message
response = requests.get(send_text)
print(response.json())
else:
print("succ")
browser.close()
但現在我收到以下錯誤:
Traceback (most recent call last):
File "./ajax.py", line 18, in <module>
browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 191, in __init__
self.binary, timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 104, in _wait_until_connectable
"The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
也更改cap["marionette"] = False為True只是給我舊的錯誤訊息。
謝謝你!
uj5u.com熱心網友回復:
我只是恢復了所有更改,制作了一個 Docker 容器并將其放在服務器上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314691.html
