使用Python Requests模块无法访问https://99acres.com的问题求助
使用Python Requests访问99acres.com的可行方案
99acres.com存在反爬机制,仅添加User-Agent不足以绕过检测,以下是几种可行的解决方法:
方法1:补充完整请求头并设置超时
模拟更接近浏览器的请求头,同时避免无限等待:
import requests url = 'https://99acres.com' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', 'DNT': '1', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-User': '?1' } try: response = requests.get(url, headers=headers, timeout=10, allow_redirects=True) response.raise_for_status() print("请求成功,状态码:", response.status_code) print(response.text[:500]) # 打印前500字符验证 except requests.exceptions.RequestException as e: print("请求失败:", str(e))
方法2:改用支持HTTP/2的库
99acres可能优先支持HTTP/2协议,而Requests默认使用HTTP/1.1,可改用httpx库(需先安装:pip install httpx[http2]):
import httpx url = 'https://99acres.com' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Upgrade-Insecure-Requests': '1' } with httpx.Client(http2=True, timeout=10) as client: response = client.get(url, headers=headers) print("请求成功,状态码:", response.status_code) print(response.text[:500])
方法3:添加代理IP(若IP被封禁)
如果本地IP被网站限制,可配置代理:
import requests url = 'https://99acres.com' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', # 其他请求头同方法1 } # 替换为可用的代理地址 proxies = { 'http': 'http://your-proxy-address:port', 'https': 'http://your-proxy-address:port' } try: response = requests.get(url, headers=headers, timeout=10, proxies=proxies) response.raise_for_status() print("请求成功") except requests.exceptions.RequestException as e: print("请求失败:", str(e))
方法4:复用浏览器Cookies
从浏览器开发者工具中复制当前Cookies,添加到请求头中:
headers['Cookie'] = '复制的浏览器Cookies字符串'
内容的提问来源于stack exchange,提问作者Manjushri
相关产品推荐
相关产品推荐

