Selenium + ChromeDriver 卡住了整整 4 分钟

Selenium + ChromeDriver 卡住了整整 4 分钟

我正在尝试从远程服务器抓取具有 CloudFlare 保护的网页:

#!/usr/bin/env python3

from xvfbwrapper import Xvfb
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import logging
display = Xvfb()
display.start()

try:
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
    logger = logging.getLogger('main')
    chrome_options = Options()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-setuid-sandbox")
    browser = webdriver.Chrome(chrome_options=chrome_options)
    browser.get('url ...')
    time.sleep(10)
    logger.info('woke up')
    logger.info(browser.get_cookies())
finally:
    display.stop()

问题是 get_cookies 调用阻塞了整整 4 分钟。此外,如果我尝试继续遍历网站,我也会在每次向服务器发出请求时阻塞整整 4 分钟:

root@server:~# ./s.py
2019-02-05 00:52:11,509 - main - INFO - woke up
2019-02-05 00:56:06,506 - main - INFO - [some cookies ...]

我尝试在脚本卡住时终止它,结果发现代码卡在这里:

root@server:~# ./s.py
2019-02-05 01:00:31,333 - main - INFO - woke up
^CTraceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./s.py", line 22, in <module>
logger.info(browser.get_cookies())
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 841, in get_cookies
    return self.execute(Command.GET_ALL_COOKIES)['value']
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/usr/lib/python3/dist-packages/urllib3/request.py", line 66, in request
**urlopen_kw)
  File "/usr/lib/python3/dist-packages/urllib3/request.py", line 87, in request_encode_url
return self.urlopen(method, url, **extra_kw)
  File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 321, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt

我还尝试使用 strace 附加到脚本并发现该脚本在从套接字获取数据包时被阻止:

root@server:~# strace -p 7545
strace: Process 7545 attached
recvfrom(4, 0x1ec58f0, 8192, 0, NULL, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)

我尝试使用不同版本的 Chrome/Selenium,结果始终相同:坚持 4 分钟。

Ubuntu 18.04.1 LTS

Google Chrome 66.0.3359.181

ChromeDriver 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)

名称:selenium 版本:3.141.0

名称:xvfbwrapper 版本:0.2.9

相关内容