2017-10-16 51 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

相关问题