【问题标题】:Twitter no longer works with requests library pythonTwitter 不再适用于请求库 python
【发布时间】:2020-09-22 15:15:48
【问题描述】:

我有一个 python 函数,它使用 requests 库和 BeautifulSoup 来抓取特定用户的推文。

import requests
from bs4 import BeautifulSoup

contents = requests.get("https://twitter.com/user")
soup = BeautifulSoup(contents.text, "html.parser")

当请求库访问 Twitter 时,它使用 Twitter 的旧版本。然而,由于 Twitter 最近放弃了对其旧版本的支持,请求库不再工作,并返回 html 代码,说明此版本的 Twitter 已过时。

有没有办法让请求库访问更新版本的 Twitter?

【问题讨论】:

    标签: python twitter python-requests twitterapi-python


    【解决方案1】:

    requests 库将访问您传递给它的 URL。我建议检查Twitter API Docs 并更新您的代码以对应最新版本。

    【讨论】:

      【解决方案2】:

      无法直接回答(也没有足够的评论点),但遇到同样的问题,我确实找到了一些新工具。 https://github.com/bisguzar/twitter-scraper 使用 requests_html 来获取推文(参见他们的 tweets.py 模块)。而https://github.com/Mottl/GetOldTweets3/ 是另一个用于抓取推文的强大 python 工具。

      【讨论】:

        【解决方案3】:

        我也遇到过这个问题。造成这种情况的根本原因是 Twitter 拒绝“旧版”浏览器,不幸的是其中包含 Python 的 requests 库。

        Twitter 通过查看作为请求的一部分发送的 User-Agent 标头来确定您使用的浏览器。所以我对这个问题的解决方案是简单地欺骗这个标题。

        在您的特定情况下,请尝试执行以下操作;

        import requests
        from bs4 import BeautifulSoup
        
        contents = requests.get(
            "https://twitter.com/user",
            headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"}
        )
        soup = BeautifulSoup(contents.text, "html.parser")
        

        【讨论】:

          猜你喜欢
          • 2019-01-08
          • 1970-01-01
          • 2022-01-17
          • 1970-01-01
          • 2020-11-08
          • 2017-12-23
          • 2019-12-18
          • 2018-11-21
          相关资源
          最近更新 更多