gpt4 book ai didi

python - Apache 403代码与python解析

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:32 25 4
gpt4 key购买 nike

我想用python解析https站点。

import requests

url = 'https://example.com/wap/auth/?serviceId=121&returnUrl=https://example.com'

headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8,ru;q=0.6,de;q=0.4,sr;q=0.2',
'Connection': 'keep-alive',
'DNT': '1',
'Host': 'example.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/47.0.2526.73 Chrome/47.0.2526.73 Safari/537.36',
}

response = requests.get(url, verify=False, headers=headers)

print response.text

答案:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /wap/auth/
on this server.</p>
<hr>
<address>Apache Server at example.com Port 443</address>
</body></html>

但是此链接在浏览器中正常打开,带有 200 http 代码,无需重定向。 url 和 headers 从浏览器复制到 python 脚本。 python 脚本中没有重定向。没有代理。浏览器和 python 脚本正在我的计算机上运行(linux mint 17)。

为什么 apache 禁止了我的脚本并且可以在浏览器中正常工作?是否可以从 python 脚本发出此请求?

注意:域名已更改为 example.com

最佳答案

虽然我不太确定 urllib 的问题是什么......我可以建议一个替代方案。使用 Selenium

Selenium 可以像 Firefox 所看到的那样加载页面。它基本上使用 firefox 的加载器并加载页面,包括 JS 等 - 使自动化任务更加容易。

from selenium import webdriver
url = 'http://login.tele2.ru/ssotele2/wap/auth/'
driver = webdriver.Firefox()
driver.get(url)
print driver.page_source

关于python - Apache 403代码与python解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com