【问题标题】:Can't print text inside 'p' tag using BeautifulSoup无法使用 BeautifulSoup 在“p”标签内打印文本
【发布时间】:2020-11-03 07:19:48
【问题描述】:

很简单的代码

import requests
from bs4 import BeautifulSoup

link = 'https://www.birdsnest.com.au/brands/boho-bird/73067-amore-wrap-dress'
page = requests.get(link)
soup = BeautifulSoup(page.content, 'html.parser')
page_new = soup.find('div', class_='model-info clearfix')
results = page_new.find_all('p')
for result in results:
    print(result.text)

输出

usually wears a size .
                She is wearing a size  in this style.
              
Her height is .

Show ’s body measurements

问题在于模型的名称在<strong> 标签内,span<strong> 标签内。 像这样。

<div class="model-info-header">
              <p>
                <strong><span class="model-info__name">Marnee</span></strong> usually wears a size <strong><span class="model-info__standard-size">8</span></strong>.
                She is wearing a size <strong><span class="model-info__wears-size">10</span></strong> in this style.
              </p>
              <p class="model-info-header__height">Her height is <strong><span class="model-info__height">178 cm</span></strong>.</p>
              <p>
                <span class="js-model-info-more model-info__link model-info-header__more">Show <span class="model-info__name">Marnee</span>’s body measurements</span>
              </p>
            </div>

如何获取&lt;p&gt;标签内的BOLD元素。

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    模型名称是动态生成的。试试这个:

    from bs4 import BeautifulSoup
    from selenium import webdriver
    import time
    
    link = 'https://www.birdsnest.com.au/brands/boho-bird/73067-amore-wrap-dress'
    
    driver = webdriver.Chrome()
    driver.get(link)
    time.sleep(3)
    
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    driver.close()
    page_new = soup.find('div', class_='model-info clearfix')
    results = page_new.find_all('p')
    for result in results:
        print(result.text)
    

    输出:

    Marnee usually wears a size 8.
                    She is wearing a size 10 in this style.
                  
    Her height is 178 cm.
    Show Marnee’s body measurements
    Marnee’s body measurements are:
    Bust 81 cm
    Waist 64 cm
    Hips 89 cm
    

    【讨论】:

    • 我必须使用硒吗?这不能通过请求来完成吗?
    • 当您执行requests.get 时,p 标签中不存在型号名称。这意味着它是动态加载的。所以你必须使用selenium。没别的了。
    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2010-10-06
    • 2020-11-14
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多