我試圖從Twitter搜索的結果中刮取喜歡和轉推。使用Python在Twitter中刮取嵌套的Div和Spans?
運行下面的Python之後,我得到一個空列表[]
。我沒有使用Twitter API,因爲它沒有通過這個標籤來查看推文。
我正在使用的代碼是:
from bs4 import BeautifulSoup
import requests
url = 'https://twitter.com/search?q=%23bangkokbombing%20since%3A2015-08-10%20until%3A2015-09-30&src=typd&lang=en'
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")
all_likes = soup.find_all('span', class_='ProfileTweet-actionCountForPresentation')
print(all_likes)
我可以成功地保存HTML使用此代碼文件。當我搜索文本時缺少大量的信息,例如我正在尋找的類名...
所以(部分)問題顯然是在準確地訪問源代碼。
filename = 'newfile2.txt'
with open(filename, 'w') as handle:
handle.writelines(str(data))
此屏幕截圖顯示了我試圖抓取的範圍。
我看這個問題,以及其他類似的話,但我不是很到達那裏。
How can I use BeautifulSoup to get deeply nested div values?
*謝謝* a)知道twitter有API,b)解釋你爲什麼不使用它。 – roippi