Python 如何从隐藏的跨类HTML中刮取链接?

Python 如何从隐藏的跨类HTML中刮取链接?,python,html,selenium,hover,Python,Html,Selenium,Hover,我正在学习网络抓取,就像我从真实网站抓取真实世界的数据一样。 然而,到目前为止,我从未遇到过此类问题。 人们通常可以通过右键单击网站部分,然后单击inspect选项来搜索想要的HTML源代码。我马上跳到这个例子来解释这个问题 从上图中可以看出,红色标记的span类原来并不存在,但当我将光标放在(甚至没有单击)用户名上时,会弹出一个小框,显示该用户的span类。我最终想要刮取的是嵌入在该span类中的用户配置文件的链接地址。我不确定,但如果我能解析该span类,我想我可以尝试刮取链接地址,但我一

我正在学习网络抓取,就像我从真实网站抓取真实世界的数据一样。 然而,到目前为止,我从未遇到过此类问题。 人们通常可以通过右键单击网站部分,然后单击inspect选项来搜索想要的HTML源代码。我马上跳到这个例子来解释这个问题

从上图中可以看出,红色标记的span类原来并不存在,但当我将光标放在(甚至没有单击)用户名上时,会弹出一个小框,显示该用户的span类。我最终想要刮取的是嵌入在该span类中的用户配置文件的链接地址。我不确定,但如果我能解析该span类,我想我可以尝试刮取链接地址,但我一直无法解析隐藏的span类

我没有期望那么多,但是我的代码当然给了我一个空列表,因为当我的光标不在用户名上时,span类没有出现。但是我展示我的代码来展示我所做的

from bs4 import BeautifulSoup
from selenium import webdriver

#Incognito Mode
option=webdriver.ChromeOptions()
option.add_argument("--incognito")

#Open Chrome
driver=webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",options=option)

driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html")
time.sleep(3)

#parse html
html =driver.page_source
soup=BeautifulSoup(html,"html.parser")

hidden=soup.find_all("span", class_="ui_overlay ui_popover arrow_left")
print (hidden)
有没有简单直观的方法可以使用selenium解析隐藏的span类?如果我可以解析它,我可以使用“find”函数解析用户的链接地址,然后循环遍历所有用户以获取所有链接地址。 多谢各位

==================================更新了问题,添加了以下内容===================
为了对我要检索的内容添加更详细的解释,我想从下图中获取一个红色箭头指向的链接。谢谢你指出我需要更多的解释

===========================================到目前为止已更新代码=====================

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

#Incognito Mode
option=webdriver.ChromeOptions()
option.add_argument("--incognito")

#Open Chrome
driver=webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",options=option)

driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html")
time.sleep(3)

profile=driver.find_element_by_xpath("//div[@class='mainContent']")
profile_pic=profile.find_element_by_xpath("//div[@class='ui_avatar large']")

ActionChains(driver).move_to_element(profile_pic).perform()
ActionChains(driver).move_to_element(profile_pic).click().perform()

#So far I could successfully hover over the first user. A few issues occur after this line.
#The error message says "type object 'By' has no attribute 'xpath'". I thought this would work since I searched on the internet how to enable this function.
waiting=wait(driver, 5).until(EC.element_to_be_clickable((By.xpath,('//span//a[contains(@href,"/Profile/")]'))))

#This gives me also a error message saying that "unable to locate the element".
#Some of the ways to code in Python and Java were different so I searched how to get the value of the xpath which contains "/Profile/" but gives me an error.
profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
print (profile_box)



另外,在这种情况下,是否有任何方法可以迭代xpath?

我认为您可以使用请求库而不是selenium

当您将鼠标悬停在用户名上时,您将获得如下所示的请求URL

这是输出:

200
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
/Profile/JLERPercy

如果您想使用selenium获取弹出框

可以使用ActionChains执行hover()函数

但我认为这比使用请求效率低

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).perform()

我认为您可以使用请求库而不是selenium

当您将鼠标悬停在用户名上时,您将获得如下所示的请求URL

这是输出:

200
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_805E0639C29797AEDE019E6F7DA9FF4E-SRC_507403702
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_6A86C50AB327BA06D3B8B6F674200EDD-SRC_506453752
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_97307AA9DD045AE5484EEEECCF0CA767-SRC_500684401
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
UID_E629D379A14B8F90E01214A5FA52C73B-SRC_496284746
/Profile/JLERPercy

如果您想使用selenium获取弹出框

可以使用ActionChains执行hover()函数

但我认为这比使用请求效率低

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).perform()
python 下面的代码将提取href值。请尝试让我知道它是如何运行的

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome('/usr/local/bin/chromedriver')  # Optional argument, if not specified will search path.
driver.implicitly_wait(15)

driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html");

#finds all the comments or profile pics
profile_pic= driver.find_elements(By.XPATH,"//div[@class='prw_rup prw_reviews_member_info_hsx']//div[@class='ui_avatar large']")

for i in profile_pic:
        #clicks all the profile pic one by one
        ActionChains(driver).move_to_element(i).perform()
        ActionChains(driver).move_to_element(i).click().perform()
        #print the href or link value
        profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
        print (profile_box)

driver.quit()
Java示例: python 下面的代码将提取href值。请尝试让我知道它是如何运行的

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome('/usr/local/bin/chromedriver')  # Optional argument, if not specified will search path.
driver.implicitly_wait(15)

driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html");

#finds all the comments or profile pics
profile_pic= driver.find_elements(By.XPATH,"//div[@class='prw_rup prw_reviews_member_info_hsx']//div[@class='ui_avatar large']")

for i in profile_pic:
        #clicks all the profile pic one by one
        ActionChains(driver).move_to_element(i).perform()
        ActionChains(driver).move_to_element(i).click().perform()
        #print the href or link value
        profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
        print (profile_box)

driver.quit()
Java示例:
因此,您想知道如何在selenium中模拟“hover”用户操作,以便popover元素出现在DOM中。请让你的问题更清楚,也许更具体。你想在弹出框中检索哪些数据?@Yun嗨,Yun。我刚刚更新了关于我想检索什么的问题。@Adriano我不知道有一个悬停函数,直到你刚刚提到它。谢谢你提出来。对我认为这会起作用,因为当我把光标放在用户的个人资料上时,popover元素会出现。这会帮你吗?因此,您想知道如何在selenium中模拟“hover”用户操作,以便popover元素出现在DOM中。请让你的问题更清楚,也许更具体。你想在弹出框中检索哪些数据?@Yun嗨,Yun。我刚刚更新了关于我想检索什么的问题。@Adriano我不知道有一个悬停函数,直到你刚刚提到它。谢谢你提出来。对我认为这会起作用,因为当我把光标放在用户的个人资料上时,popover元素会出现。这会帮你吗?谢谢@Yun你的解决方案非常直观。但您的输出只提供一个链接。下面的问题是如何悬停所有用户?我一整天都想去,但没找到。当我手动悬停每个用户时,确实会显示来自网络的URL。我搜索了如何在一行中的多个元素上悬停,但没有任何明确的材料。如果我能成功地悬停所有用户,那么您的代码将工作得最好。谢谢@Yun您的解决方案非常直观。但您的输出只提供一个链接。下面的问题是如何悬停所有用户?我一整天都想去,但没找到。当我手动悬停每个用户时,确实会显示来自网络的URL。我搜索了如何在一行中的多个元素上悬停,但没有任何明确的材料。如果我能成功地悬停所有用户,那么您的代码将工作得最好。@Todd这样行吗?如果是这样的话,我能投票支持这个答案吗?@Todd,这样行吗?如果是的话,我能投票支持这个答案吗?