【问题标题】:How to get URL attach with button?如何使用按钮获取 URL 附加?
【发布时间】:2020-02-29 23:23:54
【问题描述】:

我正在抓取这个网站Click here 这是每条记录中使用 java 脚本打开 Spotify 页面的按钮。我想使用 python 获取那个 Spotify 页面的 URL。我使用 Selenium 和 Beautiful-soup 来抓取其他内容,但我找不到 scrape 该网址的方法。请任何人都可以告诉我如何做到这一点。

已编辑:

我正在尝试这个。

browser1.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[1]/div/div[3]/button').click()
link = '{}'.format(browser1.current_url)
browser1.back()

首先我单击按钮转到该页面,然后获取 url,然后返回主网页。它需要很多,因为当它回到原始网站时,它位于第一页,然后我必须转到我正在抓取的那个页码。

【问题讨论】:

  • 您能与我们分享您的尝试吗?像一段代码?
  • 是的,我当然编辑了这个问题。
  • @2437141 先生,您能帮帮我吗?/.

标签: javascript python selenium web-scraping beautifulsoup


【解决方案1】:

请尝试以下使用相对 xpath 的解决方案

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By


driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.maximize_window()

driver.get("https://playlists.bubbleapps.io/playlists")
print driver.current_url

element=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='bubble-element GroupItem group-item entry-1']//button[@class='fa fa-spotify inner-element bubble-element clickable-element']")))
element.click()
#     print(elements)
# driver.find_element_by_xpath("//div[@class='bubble-element GroupItem group-item entry-1']//button[@class='fa fa-spotify inner-element bubble-element clickable-element']").click()
print driver.current_url

输出:

https://playlists.bubbleapps.io/playlists
https://open.spotify.com/playlist/5rl5QaWjWtEPv9a057w3qc

【讨论】:

  • 谢谢先生,我正在尝试类似的解决方案,但它花了我很多时间,因为它的单页网站当我回到网站第一页上的原始 URL 时,我必须去那个页面我目前正在通过单击下一步按钮进行抓取。而且网站有很多页面。
  • 是否有其他解决方案,例如使用该按钮附加 url。
  • 如果您观察到该按钮没有链接,并且根据您的上述问题,我想我已经为您提供了答案
猜你喜欢
  • 2021-01-19
  • 2013-11-17
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多