簡體   English   中英

如何從隱藏跨度 class HTML 中抓取鏈接?

[英]how to scrape links from hidden span class HTML?

當我從真實網站上抓取真實世界的數據時,我正在學習 web 抓取。 然而,到目前為止,我從未遇到過此類問題。 通常可以通過右鍵單擊網站部分然后單擊檢查選項來搜索想要的 HTML 源代碼。 我會馬上跳到這個例子來解釋這個問題。

在此處輸入圖像描述

從上圖中,紅色標記的跨度 class 原本不存在,但是當我將(甚至沒有點擊)我的 cursor 放在用戶名上時,會彈出一個該用戶的小框,並且還會顯示跨度 ZA2F2ED4F8DCEBC2CBBD4ZC21A26。 我最終想要抓取的是嵌入在該跨度 class 內的用戶配置文件的鏈接地址。我不確定,但如果我可以解析該跨度 class,我想我可以嘗試抓取鏈接地址,但我保留未能解析該隱藏跨度 class。

我沒想到那么多,但我的代碼當然給了我一個空列表,因為當我的 cursor 不在用戶名上時,跨度 class 沒有出現。 但我展示我的代碼來展示我所做的事情。

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 解析隱藏跨度 class? 如果我能解析它,我可以使用'find' function 來解析用戶的鏈接地址,然后遍歷所有用戶以獲取所有鏈接地址。 謝謝你。

=======================通過添加以下內容更新了問題===================
為了對我想要檢索的內容添加一些更詳細的解釋,我想從下圖中獲取用紅色箭頭指向的鏈接。 感謝您指出我需要更多解釋。

在此處輸入圖像描述

===========================到目前為止更新的代碼==================== =

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。

當您在用戶名上使用 hover 時,您將收到如下請求 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'))

這是 output:

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() function。

但我認為它比使用請求效率低。

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 示例:

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html");

        //finds all the comments or profiles
        List<WebElement> profile= driver.findElements(By.xpath("//div[@class='prw_rup prw_reviews_member_info_hsx']//div[@class='ui_avatar large']"));

        for(int i=0;i<profile.size();i++)
        {
            //Hover on user profile photo
            Actions builder = new Actions(driver);
            builder.moveToElement(profile.get(i)).perform();
            builder.moveToElement(profile.get(i)).click().perform();
            //Wait for user details pop-up
            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span//a[contains(@href,'/Profile/')]")));
            //Extract the href value
            String hrefvalue=driver.findElement(By.xpath("//span//a[contains(@href,'/Profile/')]")).getAttribute("href");
            //Print the extracted value
            System.out.println(hrefvalue);
        }
        //close the browser
        driver.quit(); 

    }

}

output

 https://www.tripadvisor.com/Profile/861kellyd https://www.tripadvisor.com/Profile/JLERPercy https://www.tripadvisor.com/Profile/rayn817 https://www.tripadvisor.com/Profile/grossla https://www.tripadvisor.com/Profile/kapmem

刮<div<span from html-page< div><div id="text_translate"><p> 我正在嘗試使用 Eclipse 中的 Python 創建一個簡單的天氣預報。 到目前為止,我已經寫了這個:</p><pre> from bs4 import BeautifulSoup import requests def weather_forecast(): url = 'https://www.yr.no/nb/v%C3%A6rvarsel/daglig-tabell/1-92416/Norge/Vestland/Bergen/Bergen' r = requests.get(url) # Get request for contents of the page print(r.content) # Outputs HTML code for the page soup = BeautifulSoup(r.content, 'html5lib') # Parse the data with BeautifulSoup(HTML-string, html-parser) min_max = soup.select('min-max.temperature') # Select all spans with a "min-max-temperature" attribute print(min_max.prettify()) table = soup.find('div', attrs={'daily-weather-list-item__temperature'}) print(table.prettify())</pre><p> 從具有如下元素的 html 頁面:</p><p> <a href="https://i.stack.imgur.com/liV2d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/liV2d.png" alt=""></a></p><p> 我在 HTML 頁面的元素中找到了第一個溫度的路徑,但是當我嘗試執行我的代碼並打印以查看我是否正確完成時,沒有打印任何內容。 我的目標是打印一張帶有日期和相應溫度的表格,這似乎是一項簡單的任務,但我不知道如何正確命名屬性或如何在一次迭代中從 HTML 頁面中將它們全部刮掉。</p><p> &lt;span 存儲了兩個溫度,一個最小值和一個最大值,這里只是碰巧它們是相同的。</p><p> 我想將 go 放入每個 &lt;div class="daily-weather-list-item__temperature" 中,收集兩個溫度並將它們添加到字典中,我該怎么做?</p><p> 我已經在 stackoverflow 上查看了這個問題,但我無法弄清楚: <a href="https://stackoverflow.com/questions/53084902/python-beautifulsoup-scraping-div-spans-and-p-tags-also-how-to-get-exact-mat" rel="nofollow noreferrer">Python BeautifulSoup - Scraping Div Spans 和 p 標簽 - 以及如何在 div 名稱上獲得完全匹配</a></p></div></div<span>

[英]Scrape <div<span from HTML-page

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何從“span”內的 html“類”中獲取/抓取所有元素? 如何從隱藏的 div 類中抓取圖片? 刮<div<span from html-page< div><div id="text_translate"><p> 我正在嘗試使用 Eclipse 中的 Python 創建一個簡單的天氣預報。 到目前為止,我已經寫了這個:</p><pre> from bs4 import BeautifulSoup import requests def weather_forecast(): url = 'https://www.yr.no/nb/v%C3%A6rvarsel/daglig-tabell/1-92416/Norge/Vestland/Bergen/Bergen' r = requests.get(url) # Get request for contents of the page print(r.content) # Outputs HTML code for the page soup = BeautifulSoup(r.content, 'html5lib') # Parse the data with BeautifulSoup(HTML-string, html-parser) min_max = soup.select('min-max.temperature') # Select all spans with a "min-max-temperature" attribute print(min_max.prettify()) table = soup.find('div', attrs={'daily-weather-list-item__temperature'}) print(table.prettify())</pre><p> 從具有如下元素的 html 頁面:</p><p> <a href="https://i.stack.imgur.com/liV2d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/liV2d.png" alt=""></a></p><p> 我在 HTML 頁面的元素中找到了第一個溫度的路徑,但是當我嘗試執行我的代碼並打印以查看我是否正確完成時,沒有打印任何內容。 我的目標是打印一張帶有日期和相應溫度的表格,這似乎是一項簡單的任務,但我不知道如何正確命名屬性或如何在一次迭代中從 HTML 頁面中將它們全部刮掉。</p><p> &lt;span 存儲了兩個溫度,一個最小值和一個最大值,這里只是碰巧它們是相同的。</p><p> 我想將 go 放入每個 &lt;div class="daily-weather-list-item__temperature" 中,收集兩個溫度並將它們添加到字典中,我該怎么做?</p><p> 我已經在 stackoverflow 上查看了這個問題,但我無法弄清楚: <a href="https://stackoverflow.com/questions/53084902/python-beautifulsoup-scraping-div-spans-and-p-tags-also-how-to-get-exact-mat" rel="nofollow noreferrer">Python BeautifulSoup - Scraping Div Spans 和 p 標簽 - 以及如何在 div 名稱上獲得完全匹配</a></p></div></div<span> 如果存在相同 class 名稱的跨度,如何刮擦跨度 class 文本? 如何用同一類刮掉另一個跨度 如何從鏈接列表中抓取? 如何在另一個跨度 class 內刮掉一個跨度? 如何使用BeautifulSoup在HTML中抓取鏈接 嘗試通過 class 抓取 HTML 跨度值,但返回錯誤 使用beautifulsoup python在span類HTML中刮取值
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM