我正试图从twitter上抓取tweets作为一个辅助项目。你知道吗
在输出方面有困难。你知道吗
使用最新版本的pycharm。你知道吗
import urllib
import urllib.request
from bs4 import BeautifulSoup
theurl = "https://twitter.com/search?q=ghana%20and%20jollof&src=typed_query"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")
i = 1
for tweets in soup.findAll('div', {
"class": "css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0"
}):
print (i)
print (tweets.find('span').text)
i = i+1
print(tweets)
我没有收到任何错误,但没有任何输出的推文。你知道吗
您应该使用请求库,而且您的请求中缺少user-agent头,这对于twitter来说似乎是必需的。你知道吗
下面是一个工作示例:
问题是,这样每个请求只加载20条tweet,您需要检查network选项卡并查看浏览器如何动态加载其余的tweet。你知道吗
但是这是非常乏味的,我强烈建议使用一个直接调用twitterapi的库,比如https://github.com/twintproject/twint
相关问题 更多 >
编程相关推荐