gpt4 book ai didi

python - 如何使用Python在BeautifulSoup中提取同一div中具有相同标签的元素?

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:28 25 4
gpt4 key购买 nike

我是一个通过小项目学习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()

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

最佳答案

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")

关于python - 如何使用Python在BeautifulSoup中提取同一div中具有相同标签的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57005272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com