【问题标题】:How to Loop through with Beautiful Soup to put table text in a data frame (Python)如何使用 Beautiful Soup 循环将表格文本放入数据框中(Python)
【发布时间】:2022-06-10 20:00:08
【问题描述】:

这是我试图从中抓取的页面的链接:https://churchdwight.com/ingredient-disclosure/antiperspirant-deodorant/40002569-ultramax-clear-gel-cool-blast.aspx

这是我的代码:

''' #从 CHD 网站抓取数据表 #加载CHD网站HTML代码 结果 = requests.get(current_url, verify=False, headers={'User-Agent' : "Magic Browser"})

#Check and see if the page successfully loaded
result_status = result.status_code
                      
if result.status_code == 200:
                      
    #Extract the HTML code and pass it through beautiful soup
    source = result.content
    document = BeautifulSoup(source, 'lxml')

    #Since each page has one table for each product, we can use the table attribute to find the table
    check = 0
    table = document.find("table")
    
    while check <= 0:
        
        #Check to make sure that you got the right table by checking whether the text within the first header title is 'INGREDIENT'
        if table.find("span").get_text() == "INGREDIENT NAME":
            check += 1
        else:
            table = table.find_next("table")
            

    #Since HTML uses tr for rows, we can use find all to get our rows
    rows = table.find_all('span', style ='font-size:13px;font-family:"Arial",sans-serif;')
        
    
    #Loop through the rows
    for row in rows[3:]:
        bar = row.find('span', style ='font-size:13px;font-family:"Arial",sans-serif;')
        bar_text = row.get_text(strip = True)
        cells_names.append(bar_text)
        
    
    data_pandas = pd.DataFrame(cells_names, columns = ['Ingredients'])
    return data_pandas
   

else:
    #Print out an error if the result status is not 200
    print("Status error" + "  " + str(result_status) + "has occurred!")

'''

我的数据框中缺少润滑剂/乳化剂,我认为这是因为跨度样式有一个额外的说明颜色:黑色;背景:白色

任何帮助将不胜感激!!!!

【问题讨论】:

    标签: dataframe loops beautifulsoup


    【解决方案1】:

    只能使用pandas来抓取表数据

    import pandas as pd
    df =pd.read_html('https://churchdwight.com/ingredient-disclosure/antiperspirant-deodorant/40002569-ultramax-clear-gel-cool-blast.aspx')[2]
    print(df)
    

    输出:

    0                            INGREDIENT NAME                            FUNCTION
    1                                      Water                             Solvent
    2                         Cyclopentasiloxane                Lubricant/emulsifier
    3                              SD Alcohol 40                        Drying agent
    4                           Propylene glycol                           Humectant
    5                                Dimethicone                     Skin protectant
    6                  PEG/PPG-18/18 dimethicone                          Emulsifier
    7           Sodium bicarbonate (baking soda)                          Deodorizer
    8                                  Fragrance                           Fragrance
    9  Aluminium zirconium tetrachlorohydrex gly  Active ingredient - antiperspirant
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多