python - 如何从隐藏的跨度类 HTML 中抓取链接?
问题描述
当我从真实网站上抓取真实世界的数据时,我正在学习网络抓取。然而,到目前为止,我从未遇到过此类问题。通常可以通过右键单击网站的一部分然后单击检查选项来搜索想要的 HTML 源代码。我会马上跳到这个例子来解释这个问题。
从上图中,红色标记的跨度类最初不存在,但是当我将光标放在(甚至没有单击)用户名上时,会弹出一个该用户的小框,并且该跨度类也出现了。我最终想要抓取的是嵌入在该跨度类中的用户配置文件的链接地址。我不确定但如果我可以解析该跨度类,我想我可以尝试抓取链接地址但我保留无法解析隐藏的跨度类。
我没想到那么多,但我的代码当然给了我一个空列表,因为当我的光标不在用户名上时,跨度类没有出现。但我展示我的代码来展示我所做的事情。
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 解析隐藏的跨度类?如果我可以解析它,我可以使用“查找”函数来解析用户的链接地址,然后遍历所有用户以获取所有链接地址。谢谢你。
=======================通过添加下面的更新问题===================
添加关于我要检索的内容的一些更详细的解释,我想从下图中获取用红色箭头指向的链接。感谢您指出我需要更多解释。
===========================到目前为止更新的代码==================== =
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 吗?
解决方案
我认为您可以使用请求库而不是硒。
当您将鼠标悬停在用户名上时,您将获得如下所示的请求 URL。
import requests
from bs4 import BeautifulSoup
html = requests.get('https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html')
print(html.status_code)
soup = BeautifulSoup(html.content, 'html.parser')
# Find all UID of username
# Split the string "UID_D37FB22A0982ED20FA4D7345A60B8826-SRC_511863293" into UID, SRC
# And recombine to Request URL
name = soup.find_all('div', class_="memberOverlayLink")
for i in name:
print(i.get('id'))
# Use url to get profile link
response = requests.get('https://www.tripadvisor.com/MemberOverlay?Mode=owa&uid=805E0639C29797AEDE019E6F7DA9FF4E&c=&src=507403702&fus=false&partner=false&LsoId=&metaReferer=')
soup = BeautifulSoup(response.content, 'html.parser')
result = soup.find('a')
print(result.get('href'))
这是输出:
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()
推荐阅读
- nuxt.js - Nuxt Capacitor Android,但不是静态站点
- python - 在循环中创建 QPixmap 会占用大量 RAM
- flutter - '未来
' 不是类型转换中类型 'GeoFirePoint' 的子类型 - javascript - 按钮单击未在模式窗口中触发
- arrays - Laravel 8 array_key_exists() 中的数组键错误
- git - 我如何在git中切换分支?
- javascript - 在猫鼬中实现大小为 N 的队列
- python - python关于切换线程的逻辑是什么?
- java - 尝试从 Eclipse 市场安装时卡住(错误:正在解决功能)
- javascript - 我们可以在另一台服务器的频道中检查成员权限吗?(discord.js v12)