【问题标题】:requests.get() method times out even though website is clearly online即使网站明显在线,requests.get() 方法也会超时
【发布时间】:2016-09-17 17:51:00
【问题描述】:

所以,我决定通过学习一门新语言并开始构建机器人来提高我的编程技能。 “hackthissite.org”有几个我想完成的编程挑战。第一个是解读一些单词。

好的,很简单。让我创建一个脚本,首先登录并隔离单词。

我似乎无法在 15 秒内连接到该网站。我正在使用“请求”API 来执行此操作。这是我的代码:

def main():
print("Starting Prograam")
session = requests.session()
session = requests.get("https://www.hackthissite.org/pages/index/index.php")
print(str(session.status_code))
print("Successfully Connected to the site") #TODO: Error Handling
login = {'username' : 'My Account Username', 'password' : 'Terrible Hard-coded Password Here'}
session = requests.post("https://www.hackthissite.org/user/login", data=login)
bs = BeautifulSoup(session.content, "html.parser")
print(bs.prettify())

main()

该程序运行了 11 年,但我最终得到了超时错误或等待了一段我知道我不应该等待的荒谬时间。我似乎无法在互联网上找到和我有同样问题的人。 “hackthissite.org”是针对机器人的吗?我是否需要以某种方式掩盖我作为用户的活动?

【问题讨论】:

    标签: python-3.x http python-requests bots


    【解决方案1】:

    您从不使用您第一次创建的会话,因此最后两行的所有行都无关紧要,登录您只需要您的用户名、密码并将 referer 标头设置为https://www.hackthissite.org:

    def main():
        print("Starting Program")
        with  requests.session() as session:
            login = {"username": "username",
                     "password": "pass",
                     "btn_submit": "Login"}
            session.headers.update({"referer":"https://www.hackthissite.org"})
            s = session.post("https://www.hackthissite.org/user/login", data=login)
            bs = BeautifulSoup(s.content, "html.parser")
            print(bs.prettify())
    

    完成后,您将在输出中看到您的个人资料页面。

    【讨论】:

    • 好的,现在我一直在登录,但只是在大约 15 秒之后。是什么赋予了?我可以比这更快地手动登录。
    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 2014-02-14
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2014-04-24
    • 2021-11-23
    相关资源
    最近更新 更多