屏幕抓取推文仅返回20条推文

时间:2015-11-07 21:18:31

标签: python html twitter

我尝试使用BeautifulSoup4从用户个人资料(输入:用户名)的源页面进行抓取。  这是我的代码:

import re
import requests
from bs4 import BeautifulSoup
import webbrowser

def getTweets(usrUrl):
    """Collects the tweets of passed @username and returns a list of tweets"""
    r = requests.get(usrUrl)
    soup = BeautifulSoup(r.content)
    tweetStream = soup.find_all('ol', {'id': 'stream-items-id'})[0].find_all('li', {'data-item-type': 'tweet'})
    for tweetTree in tweetStream:
        try:
            tweetPTags = tweetTree.div.find('div', {'class':'content'}).find_all('p')
            tweets = []
            for tag in tweetPTags:
                tweets.append(tag.text)
        except:
            pass
    return tweets

usrNm = raw_input('Provide your username: @')
usrUrl = 'https://twitter.com/' + usrNm.lower()
followersUrl = usrUrl + '/followers'
usrTweets = getTweets(usrUrl)
for tweet in UsrTweets:
    print tweet
    print '\n'

但是,我收到的推文不超过20条。我曾尝试过以前的类似问题,但对它们的理解并不多。我试图不使用Twitter API来达到它的速率限制,而且就个人而言,这是我第一次在线搜索,所以我想在不使用第三方刮刀的情况下这样做。

在检查源页面时,这是我观察到的。推文流作为列表存在,列表的最后两个元素与其他18个元素不同:

Ordered List of tweets - before scrolling(抱歉,不要声誉为10,所以无法在此处发布图片。)

然而,当我向下滚动到最后一条推文时,页面加载,自动发送更多推文,源页面现在看起来像这样: 红色的方块会自动附加到源中。

Ordered List of tweets - after scrolling

所以我想知道新的推文流是否与标签的最后一个元素有关:<ol id="stream-items-id"></ol>实际上,它包含js-no-dedup has-scroll-bump,当用户向下滚动到时,它会加载更多的推文底部。

此问题的解决方法是什么?感谢您的帮助!

Souradeep

0 个答案:

没有答案