【问题标题】:How can I webscrape two different class elements that share the same name?我如何抓取两个具有相同名称的不同类元素?
【发布时间】:2021-10-13 13:38:58
【问题描述】:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get('https://soundcloud.com/jujubucks')
print(driver.title)

search = driver.find_element_by_class_name('soundTitle__usernameText')
search_song = driver.find_element_by_xpath('//span[@class=""]')
search_date = driver.find_element_by_class_name('sc-visuallyhidden')

print(f'''

Artist: {search.text}
Song Title: {search_song.text}
Upload Date: {search_date.text}

''')

search_plays = driver.find_element_by_css_selector('soundStats sc-ministats-group')
for search_play in search_plays:
stats = search_plays.find_element_by_class_name('sc-visuallyhidden')
print(f'''
Track Plays: {stats.text}
    ''')

driver.quit()

曲目播放和上传日期共享相同的类名,但具有不同的数据。我尝试通过它所在的 CSS 类访问曲目播放,但它只是返回一个错误。当我使用相同的类名播放曲目时。当我不寻找它时,它只是给了我上传日期。这是它返回的错误

Stream Juju Bucks music | Listen to songs, albums, playlists for free on SoundCloud


Artist: Juju Bucks
Song Title: Squad Too Deep Ft. Cool Prince (Outro)
Upload Date: Posted 1 year ago


Traceback (most recent call last):
File "C:\Users\houst\PycharmProjects\The Machine App\Commercial Profile.py", line 27, in 
<module>
search_plays = driver.find_element_by_css_selector('soundStats sc-ministats-group')
File "C:\Users\houst\PycharmProjects\The Machine App\venv\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Users\houst\PycharmProjects\The Machine App\venv\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\houst\PycharmProjects\The Machine App\venv\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\houst\PycharmProjects\The Machine App\venv\lib\site- 
packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"css selector","selector":"soundStats sc-ministats-group"}
(Session info: chrome=92.0.4515.131)


Process finished with exit code 1

【问题讨论】:

    标签: python css selenium web-scraping element


    【解决方案1】:

    复合类需要用点连接。此外,由于您只选择顶级曲目,因此您可以添加一个子类来获取播放统计信息而无需循环:

    stats = driver.find_element_by_css_selector('.soundStats.sc-ministats-group .sc-visuallyhidden')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多