python-3.x - BeautifulSoup 解析 soundcloud 追随者
问题描述
我正在尝试解析 soundcloud 页面并从帐户“关注者”页面中获取链接和用户名。
我已经尝试了以下但我没有得到任何我想要的链接
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
driver = webdriver.Firefox(executable_path="C:\\Users\\marco\\Downloads\\geckodriver-
v0.23.0-win64\\geckodriver.exe")
driver.get("https:////soundcloud.com/marco-valencia/followers")
soup = BeautifulSoup(driver.page_source)
print(soup.find_all("a"))
我想找到类“userBadgeListItem__image”的所有“a”并提取href字符串和相应的链接。
解决方案
让 BeautifulSoup 找到a
具有类的元素
soup.find_all("a", class_="userBadgeListItem__image")
但是现在让我们只使用 Selenium。该类userBadgeListItem__image
没有锚文本将其更改为userBadgeListItem__heading
driver.get("https://..............")
# scroll down to get all followers
while True:
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(3) # wait ajax request
try:
# loop until this loading element removed from the page
driver.find_element_by_css_selector('div.loading.regular.m-padded')
except: break
# finally extract the followers
followers = driver.find_elements_by_class_name('userBadgeListItem__heading')
for f in followers:
print('%s: %s' % (f.text, f.get_attribute('href')))
推荐阅读
- python - 如何在python中使用列表索引号访问字典值?
- android - 如何将 Android APK 上传到 Fabric Beta
- asp.net-mvc - Vue + Asp.net 渲染问题
- sql-server - 删除多个数据库是否会删除已经存在的 SQL Server 代理作业?
- sql - 查询将两条记录合并为一条
- optimization - Pyomo:如何为每个 (i,j) 对编写约束
- css - Ruby on Rails 6 how to reference images from asset pipeline for css
- botframework - AAD 身份验证 Directline & Teams,禁用安全代码
- python - 使用枕头识别数独网格中的数字
- flutter - Flutter spaceBetween 在 Card 小部件内的 Column 中不起作用