如何解决请求无法通过 VPN 运行的问题?

use*_*356 7 python python-requests anaconda

我正在尝试使用 python 中的请求来抓取网站。

url = "/sf/ask/1610925431/"
# set the headers like we are a browser,
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}

# download the homepage
s = requests.Session()
s.trust_env = False
response = s.get(url, headers=headers )
Run Code Online (Sandbox Code Playgroud)

当我使用我的个人 wifi 时,这工作正常。但是,当我连接到公司的 VPN 时,出现以下错误。

ConnectionError:HTTPSConnectionPool(host ='stackoverflow.com',port = 443):超过最大重试次数,网址:/questions/23013220/max-retries-exceeded-with-url(由NewConnectionError(':无法建立新连接引起) : [WinError 10061] 由于目标机器主动拒绝而无法建立连接',))

现在,我需要它通过我公司的 VPN 工作,因为我需要访问只能在该 VPN 中工作的网站。如何解决这个问题?

小智 3

像这样尝试怎么样:

url = "/sf/ask/1610925431/"
ua = UserAgent()
headers = headers = {"User-Agent": ua.random}

# download the homepage
s = requests.Session()
s.trust_env = False
response = s.get(url, headers=headers)
Run Code Online (Sandbox Code Playgroud)

这似乎是由于UserAgent()设置差异造成的。