Cloudscraper returns AttributeError: 'SSLContext' object has no attribute 'orig_wrap_socket'

Cloudscraper returns AttributeError: 'SSLContext' object has no attribute 'orig_wrap_socket'

import cloudscraper
import requests

scraper = cloudscraper.create_scraper()  # returns a CloudScraper instance
# Or: scraper = cloudscraper.CloudScraper()  # CloudScraper inherits from requests.Session
print (scraper.get("https://www.youtube.com/").text )

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-459-1f19dc044105> in <module>
      2 import requests
      3 
----> 4 scraper = cloudscraper.create_scraper()  # returns a CloudScraper instance
      5 # Or: scraper = cloudscraper.CloudScraper()  # CloudScraper inherits from requests.Session
      6 print (scraper.get("https://www.youtube.com/").text )

~/.local/lib/python3.6/site-packages/cloudscraper/__init__.py in create_scraper(cls, sess, **kwargs)
    315         Convenience function for creating a ready-to-go CloudScraper object.
    316         
--> 317         scraper = cls(**kwargs)
    318 
    319         if sess:

~/.local/lib/python3.6/site-packages/cloudscraper/__init__.py in __init__(self, *args, **kwargs)
    169                 server_hostname=self.server_hostname,
    170                 source_address=self.source_address,
--> 171                 ssl_context=self.ssl_context
    172             )
    173         )

~/.local/lib/python3.6/site-packages/cloudscraper/__init__.py in __init__(self, *args, **kwargs)
     75             self.ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
     76 
---> 77             self.ssl_context.orig_wrap_socket = self.ssl_context.wrap_socket
     78             self.ssl_context.wrap_socket = self.wrap_socket
     79 

AttributeError: 'SSLContext' object has no attribute 'orig_wrap_socket'

根据 https://pypi.org/project/cloudscraper/ 上的文档,代码是 运行,我找不到有关如何解决此错误消息的相关支持信息。

在 12 天前 1.2.60 发布最新版本之前,我的脚本运行良好。可能是 python3.6.

中旧库 ssl.py 的问题

这是我暂时的解决方案:

  • 使用最新版本1.2.58pip3 install cloudscraper==1.2.58
  • 或使用 python 3.7+,测试使用 python 3.7