【问题标题】:Python-Twitter APIPython-Twitter API
【发布时间】:2017-06-02 04:04:46
【问题描述】:

我想知道是否有方法可以在不使用其 API 的情况下抓取 Twitter?我尝试使用他们的 API,它很棒。不过我想问一下有没有其他选择?由于我正在开发的爬虫将被传递,我不希望我的令牌密钥在它们之间共享。我也不希望他们每个人都经历创建开发帐户的麻烦等等。

我使用 twitter API 创建的爬虫能够检索许多推文。我创建的爬虫只能爬 10 条左右,因为其他推文会在 html 之外。

我正在使用 python 3.6

def spider(targetname, DOMAIN):
for item in g_data:
    try:
        name = item.find_all("strong", {"class": "fullname show-popup-with-id "})[0].text
        username = item.find_all("span", {"class": "username u-dir"})[0].text
        post = item.find_all("p", {"class": "TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})[0].text
        retweetby = item.find_all("a", {"href": "/"+targetname})[0].text
        subdatas = item.find_all('div', {'class':'ProfileTweet-actionCountList u-hiddenVisually'})
        for subdata in subdatas:
            replies = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[0].text
            retweets = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[1].text
            likes = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[2].text
        datas = item.find_all('a', {'class':'tweet-timestamp js-permalink js-nav js-tooltip'})
        for data in datas:
            link = DOMAIN + data['href']
            date = data['title']
        if link in open(crawledfile).read():
            pass
        else:
            append_to_crawled(crawledfile, name, username, post, link, replies, retweets, likes, retweetby, date)
        output(name, username, post, link, replies, retweets, likes, retweetby, date)
    except:
        pass

【问题讨论】:

  • 没办法,绕过twitter的API。
  • 在没有 twitter API 的情况下发布你的爬虫的代码
  • @MrSam 很好,你可以使用 Selenium 和 PhantomJS。
  • 嗨@TheDarkKnight 我已经发布了正在爬行的部分。介意看看吗?

标签: python twitter


【解决方案1】:

一种无需使用 twitter API 即可抓取/scrapetwitter 的方法;但是,强烈建议您使用 API 本身。除了得到社区的大量支持之外,这还有几个优点,例如它是官方的

不过,您可以使用requestsbeautiful soup 执行抓取,或者如果您正在寻找更强大的选项,请使用SeleniumPhantomJS

您可以阅读以下几个类似的问题:

Scraping of the Twitter follower page using selenium and phantomjs

How to collect tweets about an event that are posted on specific date using python?

How to perform oauth when doing twitter scraping with python requests

Infinite Web Scraping Twitter

干杯:)

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2017-11-13
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多