情景再现
Python 爬虫请求链接:https://www.modakawa.com/products/sports-high-rise-waistband-2-in-1-shorts?variant=ea7bfab8-695c-4ab8-9b32-52c7163590ff
返回以下页面HTML信息
原因描述
该问题主要是由于站点【modakawa.com】运营商开启服务器防护导致。
解决方式
开启请求会话并且挂载适配器
import requests
import json
from requests.adapters import HTTPAdapter
from urllib3.util.ssl_ import create_urllib3_context
class CipherAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = create_urllib3_context(ciphers='DEFAULT:@SECLEVEL=2')
kwargs['ssl_context'] = context
return super(CipherAdapter, self).init_poolmanager(*args, **kwargs)
def proxy_manager_for(self, *args, **kwargs):
context = create_urllib3_context(ciphers='DEFAULT:@SECLEVEL=2')
kwargs['ssl_context'] = context
return super(CipherAdapter, self).proxy_manager_for(*args, **kwargs)
url = 'https://www.modakawa.com/products/sports-high-rise-waistband-2-in-1-shorts?variant=ea7bfab8-695c-4ab8-9b32-52c7163590ff'
client = requests.Session()
client.mount( url, CipherAdapter())
client.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
resp = client.get(url)
client.close()
result =json.loads( resp.content )
print( result )