gpt4 book ai didi

python - 使用 Beautifulsoup 抓取转发数量

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:40 26 4
gpt4 key购买 nike

您好,我正在使用 Beautifulsoup 来抓取 Twitter 数据,我想抓取每条推文的转发次数,下面是我的代码

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
您的代码几乎没有修改:

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

关于python - 使用 Beautifulsoup 抓取转发数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42273089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com