使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

你的左腿

比较初学者。有类似的主题,但我可以看到我的解决方案是如何工作的,我只需要帮助连接最后几个点。我想在使用 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 中不起作用。只是想确保此代码简洁且实用。

塞尔丘克

我不得不使用 headless 选项并添加了 executable_path 进行测试。你可以删除它。

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path="chromedriver.exe",chrome_options=options)

driver.get('https://www.instagram.com/cocacola')

soup = BeautifulSoup(driver.page_source,'lxml')

#This will give you span that has title attribute. But it gives us multiple results
#Follower count is in the inner of a tag.
followers = soup.select_one('a > span[title]')['title'].replace(',','')

print(followers)
#Output 2598552

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Selenium和BeautifulSoup输入内容来抓取网站?

使用 Python 和 BeautifulSoup 抓取 alt 标签

使用BeautifulSoup和Selenium抓取特定的html标签

使用 BeautifulSoup 来抓取带有 CSS ID 的标签

使用 python 和 BeautifulSoup 抓取不完整的标签

无法使用 Selenium 和 BeautifulSoup 抓取文本

使用 BeautifulSoup 和 Selenium 抓取数据

使用Selenium和BeautifulSoup提取iFrame内容

使用BeautifulSoup和Selenium解析HTML内容

使用python和BeautifulSoup进行网络抓取

使用 Python 和 BeautifulSoup 进行问题抓取

如何使用BeautifulSoup和Python抓取页面?

Python - 使用 BeautifulSoup 和 Urllib 进行抓取

使用 Beautifulsoup 和 Python 抓取复杂的表格

使用Python和BeautifulSoup从HTML抓取数字

使用 BeautifulSoup 和 Python 抓取表格

使用 BeautifulSoup 和 Selenium 抓取一个网站的多个网页的内容

使用来自 AJAX 网站的 selenium 和 beautifulsoup 在 python 中抓取图像

如何使用 Python、Selenium 和 BeautifulSoup 从 HTML <span id> 中抓取此文本?

使用 Selenium 和 BeautifulSoup 抓取饥饿游戏的用户评分

使用 Selenium 和 BeautifulSoup 进行 Zillow 网页抓取

使用 Selenium 和 BeautifulSoup 进行网页抓取返回空列表

如何使用 Selenium 和 BeautifulSoup 从标签中获取文本

无法使用 BeautifulSoup 定位元素和抓取内容

使用xpath php和domdocument获取特定表的内部内容来抓取数据

使用Python和BeautifulSoup抓取具有基于文本字符串的可变顺序和标签的列表

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

如何使用Python和BeautifulSoup抓取多个Google页面

使用python 2.7和beautifulsoup 4进行网站抓取