2017-02-16 122 views
1

嗨锐推我使用Beautifulsoup刮Twitter的数据的数量,我想刮锐推每个鸣叫的次数和我下面的是我的代码刮使用Beautifulsoup

import urllib2 
from bs4 import BeautifulSoup 

url = "https://twitter.com/nokia" 
response = urllib2.urlopen(url) 
soup = BeautifulSoup(response,"html.parser") 
tweets = soup.findAll('li',{"class":'js-stream-item'}) 
for tweet in tweets: 
    if tweet.find('p',{"class":'tweet-text'}): 
     tweet_user = tweet.find('span',{"class":'username'}).text 
     tweet_text = tweet.find('p',{"class":'tweet-text'}).text.encode('utf8') 
     retweets = tweet.find('span',{"class":"ProfileTweet-actionCount"}).text 
     print(tweet_user) 
     print(tweet_text) 
     print(retweets) 
    else: 
     continue 

我能够得到tweet_user和tweet_text但锐推的一些如何没能获得数可有人解释我如何虽然使用tweepy鼓励
你很少的修改代码得到锐推

+0

不是答案,但使用Twitter API可以更容易实现! – leo

+0

是的,我知道,但api有一个速度限制,而且你可以在 – srk

+0

之前不超过7天得到推文。据我所知,无论多大年纪,你都可以得到3 200条推文。至少几个星期前,我上次试过! – leo

回答

0

数:

import requests 
from bs4 import BeautifulSoup 

url = "https://twitter.com/nokia" 
response = requests.get(url) 
soup = BeautifulSoup(response.text,"lxml") 
tweets = soup.findAll('li',{"class":'js-stream-item'}) 
for tweet in tweets: 
    if tweet.find('p',{"class":'tweet-text'}): 
     tweet_user = tweet.find('span',{"class":'username'}).text.strip() 
     tweet_text = tweet.find('p',{"class":'tweet-text'}).text.encode('utf8').strip() 
     replies = tweet.find('span',{"class":"ProfileTweet-actionCount"}).text.strip() 
     retweets = tweet.find('span', {"class" : "ProfileTweet-action--retweet"}).text.strip() 
     print(tweet_user) 
     print(tweet_text) 
     print(replies) 
     print(retweets) 
    else: 
     continue 
+0

谢谢@josifoski – srk