如何将四个不同的类拼凑成一个lis

2024-04-16 22:07:34 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我基本上是想通过一个网站来制作一个显示器[Supreme restock][1]

所以现在的问题是,我可能会完全消失,但我找到的唯一解决办法是创造出这样的东西:

while True:
    try:
        list = []
        list2 = []
        list3 = []
        list4 = []

        url = 'https://www.supremecommunity.com/restocks/eu/'
        bs4 = soup(requests.get(url).text, "html.parser")

        for tag in bs4.findAll('h5', {'class': 'handle restock-name'}):
            list.append(tag.string)

        for tag2 in bs4.findAll('h6', {'class': 'restock-colorway'}):
            list2.append(tag2.string)

        for tag2 in bs4.findAll('h6', {'class': 'restock-colorway'}):
            list2.append(tag2.string)

        for tag3 in bs4.findAll('img', {'class': 'l2d-image size-thumbnail'}):
            list3.append(tag3['data-src'])

        for tag4 in bs4.findAll('div', {'class': 'message-item restock-item'}):
            itemid = tag4['data-itemid']
            list4.append('http://www.supremenewyork.com/shop/' + itemid)


        y = 0
        for x in list[:]:
            print(x + list2[y] + ' - ' + list3[y] + ' - ' + list4[y])
            y += 1

        sys.exit()

它会打印出我想要的:

Cutouts Tee( Terra Cotta - XLarge ) - http://assets.supremenewyork.com/156668/sm/laJkUkh_sRA.jpg - http://www.supremenewyork.com/shop/303505
Nylon Plaid Pullover( Green - XLarge ) - http://assets.supremenewyork.com/156221/sm/Gcd63F5PQKk.jpg - http://www.supremenewyork.com/shop/303455
Classic Script Hooded Sweatshirt( Yellow - Medium ) - http://assets.supremenewyork.com/156738/sm/sDr4Bi5w3bU.jpg - http://www.supremenewyork.com/shop/303512
Cordura® S Logo 6-Panel( Black - N/A ) - http://assets.supremenewyork.com/156721/sm/OsCNYeO_y4U.jpg - http://www.supremenewyork.com/shop/303511
Vertical Logo Baseball Jersey( Black - Medium ) - http://assets.supremenewyork.com/156286/sm/PIyVb6Gwgrk.jpg - http://www.supremenewyork.com/shop/303463
Perforated Leather Hooded Sweatshirt( Black  - Medium ) - http://assets.supremenewyork.com/156740/sm/GnenfJ06zQg.jpg - http://www.supremenewyork.com/shop/303513
Bedroom Tee( Bright Blue - Large ) - http://assets.supremenewyork.com/156682/sm/ZHITQZ65f1I.jpg - http://www.supremenewyork.com/shop/303507
Fuck You Tee( Black - Large ) - http://assets.supremenewyork.com/156653/sm/Hbytan_5dmM.jpg - http://www.supremenewyork.com/shop/303504

但我觉得这太过分了,如果有可能的话,可能更难创建一个监视器。所以我想知道我怎么能把这些都列在一个单子上,意思是

 Cutouts Tee( Terra Cotta - XLarge ) - http://assets.supremenewyork.com/156668/sm/laJkUkh_sRA.jpg - http://www.supremenewyork.com/shop/303505

所有这一切都将成为一个单一的清单等,这可能不是最好的解决方案,所以如果你有任何其他!放心吧!你知道吗


Tags: incomhttpforwwwshopclassjpg
1条回答
网友
1楼 · 发布于 2024-04-16 22:07:34

尝试查找包含给定项的名称、颜色等的容器元素,然后通过在其子元素中搜索来查找所需的属性。你知道吗

例如,对于您试图刮取的页面,它可能是div.restock-item

for item in bs4.findAll('div', {class: 'restock-item'}):
    # Filter away advertisements, which are also wrapped in `restock-item`:
    if item.find('div', {class: 'user-detail'}):
        name = item.find('h5', {'class': 'handle restock-name'}).string
        color = item.find('h6', {'class': 'restock-colorway'}).string
        # fetch thumbnails, etc. in the same fashion
        print(name + color + ...)

相关问题 更多 >