同网络下相同Python请求在PC与笔记本表现不一致问题
问题:相同请求代码在PC与笔记本上访问Cloudflare保护站点结果不一致
我编写了如下Python代码,用于向highschooltutors.com.au发送GET请求。不带请求头时请求会被拦截,Cloudflare会返回反机器人提示。
该代码在我的PC上可正常运行,但在连接同一路由器、使用相同requests版本、公网IP一致的笔记本上,使用完全相同的代码和请求头却无法正常运行。
import requests headers = { "Host": "highschooltutors.com.au", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "identity", "Sec-Ch-Ua": '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"', "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": "Windows", "Connection": "keep-alive", "Upgrade-Insecure-Requests": "1", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "same-origin", "Sec-Fetch-User": "?1", } with requests.Session() as s: response = s.get('https://highschooltutors.com.au/', headers=headers) print(response.text)
PC端成功响应(节选)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>High School tutors for all subjects near you – HighSchoolTutors.com.au</title> <meta name="description" content="Find great high school tutors for the subjects and years you need - near you, your school or online." />
笔记本端失败响应(节选)
<!DOCTYPE html><html lang="en-US"><head><title>Just a moment...</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta name="robots" content="noindex,nofollow"><meta name="viewport" content="width=device-width,initial-scale=1"><link href="/cdn-cgi/styles/challenges.css" rel="stylesheet"></head><body class="no-js"><div class="main-wrapper" role="main"><div class="main-content"><noscript><div id="challenge-error-title"><div class="h2"><span class="icon-wrapper"><div class="heading-icon warning-icon"></div></span><span id="challenge-error-text">Enable JavaScript and cookies to continue</span></div></div></noscript></div></div>
我不清楚两次请求的差异所在,它们难道不应该完全一致吗?
内容的提问来源于stack exchange,提问作者John Hon
相关产品推荐
相关产品推荐

