【发布时间】:2020-02-28 13:51:26
【问题描述】:
我在 AWS(EC2 实例)的 Ubuntu 环境中使用 Selenium + Chromedriver 时遇到问题。
我正在使用 Chromedriver Linux64 版本 (wnload chromedriver for Linux: wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip)。然后我将 Chromedriver 放入 /usr/bin。
Chrome 是使用 sudo dpkg -i google-chrome-stable_current_amd64.deb 为 Ubuntu 下载的如果我使用 google-chrome --version 验证 chrome 的版本,我看到它是:
Google Chrome 78.0.3904.70
以下 Python 代码可以工作,但问题是它只能偶尔工作。
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1420,1080')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
#Set base url (SAN FRANCISCO)
base_url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page='
#Build events array
events = []
eventContainerBucket = []
for i in range(1,2):
#cycle through pages in range
driver.get(base_url + str(i))
pageURL = base_url + str(i)
print(pageURL)
虽然上面的代码在过去没有问题,但如果我运行它几次,我最终会收到以下错误:
Traceback (most recent call last):
File "BandsInTown_Scraper_SF.py", line 84, in <module>
driver = webdriver.Chrome(chrome_options=options)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=78.0.3904.70)
我已阅读,要解决此问题,您可能需要编辑 etc/hosts 文件。我看过这里,一切看起来都不错:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
我还可以通过服务器使用请求和访问 url。例如,以下文本没有给我任何问题:
url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page=6'
res = requests.get(url)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
print(text)
我认为可能导致此问题的另一条重要信息是 Chromedriver 可能不允许在无头模式下运行。例如,如果我在终端中输入chromedriver,我会收到以下消息:
Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
最后,如果我尝试在/usr/bin 中执行chmod 777,它会显示operation not permitted。这可能是问题的一部分吗?
所以,Chrome + Chromedriver 似乎是相同的版本,所以这不是问题。 Chromedriver 和 Selenium 似乎被阻止了。我有点困惑如何解决这个问题。
【问题讨论】:
-
我会检查您是否可以使用
urllib.request或requests从服务器连接到 url。一些服务器将网络抓取视为非法(如窃取数据),因此它们可能会阻止对外部门户的访问。 -
furas,奇怪的是,这段代码已经工作,并连接到网页。只有在运行几次后它才会失败。我刚试过这个: response = requests.get('api.github.com') print(response) 我得到:
所以 - 问题似乎不在于连接到服务器,对吧? -
可能表示连接服务器没有问题。几次尝试后的问题只能意味着 github.com 可以阻止请求,如果它们过于频繁,或者它们达到了请求的某些限制,或者服务器阻止了对其 API 的不正确请求。所以 Chrome 的问题可能会有所不同。但我不知道有什么问题。也许 AWS 出于某种原因阻止了 Chrome。
-
弗拉斯,谢谢。实际上,我可以结束这个问题。看来唯一的问题是这一行,我删除了:options.add_argument("--remote-debugging-port=9222")
-
不要关闭它。添加您的评论作为答案。它可能对其他用户有用。
标签: python amazon-web-services ubuntu amazon-ec2 selenium-chromedriver