首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用美丽汤刮刮转发次数

利用美丽汤刮刮转发次数
EN

Stack Overflow用户
提问于 2017-02-16 11:47:47
回答 1查看 1.3K关注 0票数 1

嗨,我正在用Beautifulsoup来抓取twitter数据,我想为每条推文刮几个推特,下面是我的代码

代码语言:javascript
复制
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,但有些人不能得到多少转发,有人可以解释我如何获得多少转发

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-16 12:39:05

虽然我们鼓励使用tweepy

您的代码只需稍作修改:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42273089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档