【问题标题】:Extracting specific part of html提取html的特定部分
【发布时间】:2021-04-19 21:13:15
【问题描述】:

我正在使用 html 请求和漂亮的汤(对此新手)进行网络爬虫。对于 1 个网页 (https://www.selfridges.com/GB/en/cat/beauty/make-up/?pn=1),我正在尝试抓取一部分,我将为其他产品复制该部分。 html 看起来像:

<div class="plp-listing-load-status c-list-header__counter initialized" data-page-number="1" data-total-pages-count="57" data-products-count="60" data-total-products-count="3361" data-status-format="{available}/{total} results">60/3361 results</div>

我想从 data-total-pages-count="57" 中刮掉“57”。我试过使用:

soup = BeautifulSoup(page.content, "html.parser")
nopagesstr = soup.find(class_="plp-listing-load-status c-list-header__counter initialized").get('data-total-pages-count')

nopagesstr = r.html.find('[data-total-pages-count]',first=True)

但两者都返回None。我不确定如何具体选择57。任何帮助将不胜感激

【问题讨论】:

    标签: python html web-scraping beautifulsoup python-requests-html


    【解决方案1】:

    要获得总页数,您可以使用以下示例:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = "https://www.selfridges.com/GB/en/cat/beauty/make-up/?pn=1"
    
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0"
    }
    soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
    print(soup.select_one("[data-total-pages-count]")["data-total-pages-count"])
    

    打印:

    56
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多