【发布时间】:2018-08-16 01:11:38
【问题描述】:
相对初学者。有类似的主题,但我可以看到我的解决方案是如何工作的,我只需要帮助连接最后几个点。我想在不使用 API 的情况下从 Instagram 中获取关注者数量。到目前为止,这是我所拥有的:
Python 3.7.0
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
> DevTools listening on ws://.......
driver.get("https://www.instagram.com/cocacola")
soup = BeautifulSoup(driver.page_source)
elements = soup.find_all(attrs={"class":"g47SY "})
# Note the full class is 'g47SY lOXF2' but I can't get this to work
for element in elements:
print(element)
>[<span class="g47SY ">667</span>,
<span class="g47SY " title="2,598,456">2.5m</span>, # Need what's in title, 2,598,456
<span class="g47SY ">582</span>]
for element in elements:
t = element.get('title')
if t:
count = t
count = count.replace(",","")
else:
pass
print(int(count))
>2598456 # Success
有没有更简单、更快捷的方法可以到达 2,598,456 号码?我最初的希望是我可以只使用“g47SY lOXF2”类,但据我所知,类名中的空格在 BS4 中不起作用。只是想确保这段代码简洁实用。
【问题讨论】:
-
类名中的空格表示多个类。在这种情况下,您应该能够将它们拆分或剥离。
-
@MadPhysicist,我在哪里拆分?当我创建变量元素时?这要怎么写?
标签: python-3.x beautifulsoup selenium-chromedriver