在此示例中,我正在嘗試從藝術家那里收集所有 (5) 個社交媒體鏈接。目前,我的輸出只是最后一個(第五個)社交媒體鏈接。我正在使用硒,我知道這不是收集這些資料的最佳選擇,但我目前所知道的一切。請注意,我只為我的問題提供了相關代碼。提前感謝您的任何幫助/見解。
from cgitb import text
from os import link
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
from random import randint
import pandas as pd
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(chrome_options=chrome_options)
for url in urls:
driver.get(https://soundcloud.com/flux-pavilion)
time.sleep(randint(3,4))
try:
links = driver.find_elements_by_xpath('//*[@id="content"]/div/div[4]/div[2]/div/article[1]/div[2]/ul/li//a[@href]')
for elem in links:
socialmedia = (elem.get_attribute("href"))
except:
links = "none"
artist = {
'socialmedia': socialmedia,
}
print(artist)
uj5u.com熱心網友回復:
問題不在于您的 XPath 運算式,而在于您的輸出代碼的(不存在的)串列處理。
您的代碼只輸出了結果 XPath 串列的最后一項。這就是為什么您只收到一個鏈接(這是最后一個)的問題。
因此,將代碼的輸出部分更改為
[...]
url = driver.get("https://soundcloud.com/flux-pavilion")
time.sleep(randint(3,4))
artist = []
try:
links = driver.find_elements_by_xpath('//*[@id="content"]/div/div[4]/div[2]/div/article[1]/div[2]/ul/li//a[@href]')
for elem in links:
artist.append(elem.get_attribute("href"))
except:
links = "none"
for link in artist:
print(link)
并且輸出將包含您想要的所有值(鏈接):
driver = webdriver.Chrome(chrome_options=chrome_options)
https://gate.sc/?url=https%3A%2F/twitter.com/Fluxpavilion&token=da4a8d-1-1653430570528
https://gate.sc/?url=https%3A%2F/instagram.com/Fluxpavilion&token=277ea0-1-1653430570529
https://gate.sc/?url=https%3A%2F/facebook.com/Fluxpavilion&token=4c773c-1-1653430570530
https://gate.sc/?url=https%3A%2F/youtube.com/Fluxpavilion&token=1353f7-1-1653430570531
https://gate.sc/?url=https%3A%2F/open.spotify.com%2Fartist/7muzHifhMdnfN1xncRLOqk%3Fsi=bK9XeoW5RxyMlA-W9uVwPw&token=bc2936-1-1653430570532
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482300.html
