2017-10-16 50 views
0

我想從Vkontakte,俄羅斯社交網絡上的頁面中提取跟隨者計數。由於我是一名Python初學者,我曾嘗試使用我在StackOverflow中發現的代碼來初步提取Twitter上的跟隨者數量。這裏是原代碼:使用Python和BeautifulSoup從Vkontakte中提取跟隨者號碼

from bs4 import BeautifulSoup 
import requests 
username='realDonaldTrump' 
url = 'https://www.twitter.com/'+username 
r = requests.get(url) 
soup = BeautifulSoup(r.content, "html.parser") 

f = soup.find('li', class_="ProfileNav-item--followers") 
print(f) 

我使用這個網頁爲例:https://vk.com/msk_my。這裏是我的代碼:

from bs4 import BeautifulSoup 
import requests 
url = 'https://vk.com/msk_my' 
r = requests.get(url) 
soup = BeautifulSoup(r.content, "html.parser") 
f = soup.find('span', class_="header_count fl_l") 
print(f) 

此,我嘗試了很多其他變化(例如,試圖找到「格」,而不是「跨度」,僅打印「無」看來BeautifulSoup不能。找到追隨者計數,而我sttruggling明白爲什麼我已經成功地打印跟隨計數的唯一方法是這樣的:

text = soup.div.get_text() 
print(text) 

但這打印更多的東西比我想要的,我不不知道如何獲得追隨者的數量。

+0

的Twitter不允許這樣分析。使用twitter api得到你想要的任何東西 – MohitC

回答

0

試試這個吧,它只會讓你的追隨者數量。所有你需要做的就是使用硒來抓取你可以通過檢查元素看到的確切頁面源代碼。

from bs4 import BeautifulSoup 
from selenium import webdriver 

driver = webdriver.Chrome() 
driver.get('https://vk.com/msk_my') 
soup = BeautifulSoup(driver.page_source,"lxml") 
driver.quit() 
item = soup.select(".header_count")[0].text 
print("Followers: {}".format(item)) 

結果:

Followers: 59,343 
+0

非常感謝你,完美的作品。 – Pelo

相關問題