【问题标题】:How to extract elements with same tags in the same div in BeautifulSoup using Python?如何使用 Python 在 BeautifulSoup 中的同一 div 中提取具有相同标签的元素?
【发布时间】:2019-11-22 02:54:55
【问题描述】:

我是一个通过小项目学习python的初学者,所以目前正在学习使用BeautifulSoup进行网页抓取。页面的 html 如下所示:

<div class="BrandList"> <div><b>Brand Name: </b>ONCOTRON INJ</div>
 <div><b>Manufacture Name: </b>SUN PHARMA</div> <div><b>Compositions:
 </b>

Mitoxantrone 2mg/ml injection,

</div>

我需要解析信息并将其存储在包含三列的 csv 中:名称、制造商名称和成分。

我尝试运行我的代码,但我只能提取品牌名称,而我想要 div 中的剩余文本。

import requests
from bs4 import BeautifulSoup

data = requests.get ('http://www.inpharmation.in/Search/BrandList?Type=Manufacturer&ProductID=79').text
soup= BeautifulSoup(data, 'lxml')

brand = soup.find('div', attrs = {'id':'maincontent'})
out_filename = "Sunp.csv"
headers = "brand,Compositions \n"
f = open(out_filename, "w")
f.write(headers)

for BrandList in brand.findAll('div', attrs = {'class':'BrandList'}):
    BrandList['Name'] = Brand_Name.b.text
    BrandList['Compositions'] = Compositions.b.text
    print("brand: " + brand + "\n")
    print("Compositions: " + Compositions + "\n")

    f.write (brand + "," + Compositions + "\n")
f.close()

我期望输出品牌名称、成分和制造商名称,但我只得到品牌名称。

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    strip() Python 的内置函数用于从字符串中删除所有前导和尾随空格。 find_all 方法返回元素的集合。使用pandas库将数据保存到csv文件中。

    from bs4 import BeautifulSoup
    import requests
    import pandas as pd
    
    data = requests.get ('http://www.inpharmation.in/Search/BrandList?Type=Manufacturer&ProductID=79').text
    soup= BeautifulSoup(data, 'lxml')
    brand_list = soup.find_all('div', attrs = {'class':'BrandList'})
    brand_json = []
    
    for brand in brand_list:
        my_dict = {}
        brand = brand.find_all("div")
        my_dict['brand_name'] = brand[0].text.split(":")[1].strip()
        my_dict['manufacture'] = brand[1].text.split(":")[1].strip()
        my_dict['compositions'] = brand[2].text.split(":")[1].strip()
    
        brand_json.append(my_dict)
    
    print(brand_json)
    df = pd.DataFrame(brand_json)
    #save dataframe into csv file
    df.to_csv("sunp.csv")
    

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多