的Python:find_all只工作了一段标签

问题描述 投票:0回答:2

BS4内.findall功能只工作了一段HTML标签。我想凑一个网站。

from bs4 import BeautifulSoup
import requests

url = 'https://bitskins.com/'
page_response = requests.get(url, timeout=5)
page_content = BeautifulSoup(page_response.content, 'html.parser')

# Gather the two lists
skin_list = page_content.find_all('div', attrs={'class': 'panel-heading item-title'})
wear_box = page_content.find_all('div', attrs={'class': 'text-muted text-center'})

当我打印skin_list,它工作顺利,但是当我尝试打印的磨损列表,它打印一个空列表。

我曾尝试一两件事:

wear_box = page_content.html.search("Wear: {float}")

这带来了一个错误,指出“NoneType”对象不是可调用的。

我使用的崇高的文本3。

python web-scraping
2个回答
0
投票
from bs4 import BeautifulSoup
import requests

url = 'https://bitskins.com/'
page_response = requests.get(url, timeout=5)
page_content = BeautifulSoup(page_response.content, 'html.parser')

skin_list = page_content.findAll('div', class_ = 'panel item-featured panel-default')

for skin in skin_list:
    name = skin.find("div", class_ = "panel-heading item-title")
    price = skin.find("span", class_ = "item-price hidden")
    discount = skin.find("span", class_ = "badge badge-info")
    wear = skin.find("span", class_ = "hidden unwrappable-float-pointer")

    print("name:", name.text)
    print("Price", price.text)
    print("Discount:", discount.text)

    # Choose which one you want
    for w in wear.text.split(","):
        print("Wear:", w)

你试图找到不正确的类。我说,你可以废料实例的一些其他数据。戴认为这是我所输出的几个值。


0
投票

在你的代码行,你正在寻找与有多个值一类的标签。

wear_box = page_content.find_all('div', attrs={'class': 'text-muted text-center'})

在页面适合唯一的标签是:

<div class="container text-center text-muted" style="padding-top: 17px;">

在BS4,当你正在寻找具有多个值的属性,你要么寻找一个单值,如:

wear_box = page_content.find_all('p', attrs={'class': 'text-muted'})

或者你要搜索值如确切的名单:

wear_box = page_content.find_all('div', attrs={'class': 'container text-center text-muted'})
© www.soinside.com 2019 - 2024. All rights reserved.