【问题标题】:How to extract the title and src of an image with Beautifulsoup or Selenium?如何使用 Beautifulsoup 或 Selenium 提取图像的标题和 src?
【发布时间】:2021-03-26 21:11:48
【问题描述】:

所以我拥有所有页面内容:

content = driver.page_source
soup = BeautifulSoup(content, features="html.parser")

然后,我这样做了:

idioma = soup.select(".idioma > span:nth-child(1)")

这给了我这个:

[<span>
<img alt="Idioma Aleman" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ale.png" title="Idioma Aleman"/>
<img alt="Idioma Chino-tradicional" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/chi.png" title="Idioma Chino-tradicional"/>
<img alt="Idioma Coreano" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/cor.png" title="Idioma Coreano"/>
<img alt="Idioma Español" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/esp.png" title="Idioma Español"/>
<img alt="Idioma Español-latino" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/esp.png" title="Idioma Español-latino"/>
<img alt="Idioma Frances" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/fra.png" title="Idioma Frances"/>
<img alt="Idioma Ingles" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ing.png" title="Idioma Ingles"/>
<img alt="Idioma Italiano" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ita.png" title="Idioma Italiano"/>
<img alt="Idioma Portugues" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/por.png" title="Idioma Portugues"/>
<img alt="Idioma Ruso" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/rus.png" title="Idioma Ruso"/>
</span>]

当我这样做以获得标题时:

idioma = [''.join(elem.find('img')['title']) for elem in idioma if elem]

我只拿到了第一个。

['Idioma Aleman']

为什么我没有得到所有人?

【问题讨论】:

    标签: python selenium xpath beautifulsoup css-selectors


    【解决方案1】:

    为什么你没有得到所有的标题?

    这是因为 idioma 中只有一个元素并且您使用 find() 只能获得第一个匹配项。

    您可以这样做:

    idioma = [''.join(elem['title']) for elem in idioma.findAll('img')]
    print (idioma)
    

    输出

    ['Idioma Aleman', 'Idioma Chino-tradicional', 'Idioma Coreano', 'Idioma Español', 'Idioma Español-latino', 'Idioma Frances', 'Idioma Ingles', 'Idioma Italiano', 'Idioma Portugues', 'Idioma Ruso']
    

    基于评论的工作示例

    import bs4
    
    content ='''<span>
    <img alt="Idioma Aleman" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ale.png" title="Idioma Aleman"/>
    <img alt="Idioma Chino-tradicional" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/chi.png" title="Idioma Chino-tradicional"/>
    <img alt="Idioma Coreano" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/cor.png" title="Idioma Coreano"/>
    <img alt="Idioma Español" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/esp.png" title="Idioma Español"/>
    <img alt="Idioma Español-latino" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/esp.png" title="Idioma Español-latino"/>
    <img alt="Idioma Frances" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/fra.png" title="Idioma Frances"/>
    <img alt="Idioma Ingles" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ing.png" title="Idioma Ingles"/>
    <img alt="Idioma Italiano" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/ita.png" title="Idioma Italiano"/>
    <img alt="Idioma Portugues" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/por.png" title="Idioma Portugues"/>
    <img alt="Idioma Ruso" class="post_flagen" src="https://www.gamestorrents.nu/wp-content/themes/GamesTorrent/css/images/flags/rus.png" title="Idioma Ruso"/>
    </span>'''
    
    soup = bs4.BeautifulSoup(content)
    

    如下所示:

    idiomaSpan = soup.select_one('span')
    
    idioma = [''.join(elem['title']) for elem in idiomaSpan.find_all('img')]
    print (idioma)
    

    【讨论】:

    • 不行,这是控制台错误:AttributeError: ResultSet object has no attribute 'findAll'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
    • 这是由 select() 找到多个实例并返回一个列表引起的,因此您必须另外进行迭代 - 认为您可以使用 select_one() 代替,它只获得第一次出现相当于find()。在我的答案中添加了一个示例。
    【解决方案2】:

    要使用Selenium 从所有&lt;span&gt; 中提取titlesrc 属性,您必须为visibility_of_all_elements_located() 诱导WebDriverWait您可以使用以下任一Locator Strategies

    • 使用CSS_SELECTOR 表示标题

      print([my_elem.get_attribute("title") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".idioma > span:nth-child(1) img.post_flagen[alt^='Idioma']")))])
      
    • XPATH 用于src

      print([my_elem.get_attribute("src") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(@class, 'idioma')]//span//img[starts-with(@alt, 'Idioma') and @class='post_flagen']")))])
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2018-07-18
      • 2013-08-20
      • 2016-07-26
      • 1970-01-01
      • 2023-03-08
      • 2016-10-29
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多