代码之家  ›  专栏  ›  技术社区  ›  Amen Aziz

如何申请限额以获得10个结果

  •  0
  • Amen Aziz  · 技术社区  · 3 年前

    我正在努力刮 images 他们会给我 23 images 但我不想申请 limit 他们只给我10张照片你能在这些事情上帮我吗

        import requests
        from bs4 import BeautifulSoup
        import pandas as pd
        baseurl='https://twillmkt.com'
        headers ={
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
        }
        r =requests.get('https://twillmkt.com/collections/denim')
        soup=BeautifulSoup(r.content, 'html.parser')
        tra = soup.find_all('div',class_='ProductItem__Wrapper')
        productlinks=[]
        for links in tra:
            for link in links.find_all('a',href=True):
                comp=baseurl+link['href']
                productlinks.append(comp)
        
        data = []
        
        for link in set(productlinks):
            r =requests.get(link,headers=headers)
            soup=BeautifulSoup(r.content, 'html.parser')
            up = soup.find('div',class_='Product__SlideshowNavScroller')
            for e,pro in enumerate(up):
                t=pro.find('img').get('src')
                data.append({'id':t.split('=')[-1], 'image':'Image '+str(e)+' UI','link':t})
                
        df = pd.DataFrame(data)
        df.image=pd.Categorical(df.image,categories=df.image.unique(),ordered=True)
        df = df.pivot(index='id', columns='image', values='link').reset_index().fillna('')
        df.to_csv('kj.csv')
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   HedgeHog    3 年前

    对图像的结果集进行切片 [:10]

    ...
    up = soup.select('div.Product__SlideshowNavScroller img')[:10]
    for e,pro in enumerate(up):
        t=pro.get('src')
        data.append({'id':t.split('=')[-1], 'image':'Image '+str(e)+' UI','link':t})
    ...
    

    如果您想从1而不是0开始命名图像:

    ...
    up = soup.select('div.Product__SlideshowNavScroller img')[:10]
    for e,pro in enumerate(up, start=1):
        t=pro.get('src')
        data.append({'id':t.split('=')[-1], 'image':'Image '+str(e)+' UI','link':t})
    ...
    

    编辑

    基本上在excel文件中,在9个条目后,他们将在其中存储5个图像 一行和下一行中的5个图像问题是它们不能 在一行中存储10个图像

    好的,明白了——行为不是基于图像的数量,这里的问题是id不是唯一的,它不是产品的id/sku。

    如何修复?

    让我们从产品中选择sku,并将其用作数据帧中的id:

    sku = soup.select_one('.oos_sku').text.strip().split(' ')[-1]
    for e,pro in enumerate(up, start=1):
        t=pro.get('src')
        data.append({'id':sku, 'image':'Image '+str(e)+' UI','link':t})
    

    实例

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    baseurl='https://twillmkt.com'
    headers ={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
    }
    r =requests.get('https://twillmkt.com/collections/denim')
    soup=BeautifulSoup(r.content, 'html.parser')
    tra = soup.find_all('div',class_='ProductItem__Wrapper')
    productlinks=[]
    for links in tra:
        for link in links.find_all('a',href=True):
            comp=baseurl+link['href']
            productlinks.append(comp)
    
    data = []
    
    for link in set(productlinks):
        r =requests.get(link,headers=headers)
        soup=BeautifulSoup(r.content, 'html.parser')
        up = soup.select('div.Product__SlideshowNavScroller img')
        sku = soup.select_one('.oos_sku').text.strip().split(' ')[-1]
        for e,pro in enumerate(up, start=1):
            t=pro.get('src')
            data.append({'id':sku, 'image':'Image '+str(e)+' UI','link':t})
    
    df = pd.DataFrame(data)
    df.image=pd.Categorical(df.image,categories=df.image.unique(),ordered=True)
    df = df.pivot(index='id', columns='image', values='link').reset_index().fillna('')
    df#.to_excel('test.xlsx')
    

    输出

    id 图像1 UI 图像2 UI 图像3 UI 图像4 UI 图像5 UI 图6 UI 图7 UI 图8用户界面 图9 UI 图像10 UI 图11 UI 图12 UI 图13 UI 图14用户界面 图15 UI 图16 UI 图17 UI 图18 UI 图19 UI 图像20 UI 图21 UI 图22 UI 图23 UI 图像24 UI
    0 LOTFEELPJ023-30 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-2_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-3_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-4_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-5_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-6_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-7_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-8_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-9_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-10_160x.jpg?v=1631812617 //cdn.shopify.com//files/1/0089/7912/0206/products/Blue-Ripped-Knee-Distressed-Skinny-Denim-11_160x.jpg?v=1631812617
    。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。 。。。
    47 LOTFEELPJ564-S-BRN //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ564_16_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ564_17_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ564_22_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ564_15_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ564_6_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/LOTFEELPJ64_9_160x.jpg?v=1639467815 //cdn.shopify.com//files/1/0089/7912/0206/products/sizechart-store-pants_3_ec7e0b0c-1043-4306-a766-3f7e0b3edc8_160x.png?v=1639467869