【发布时间】:2020-09-29 01:34:56
【问题描述】:
我花了一整天的时间试图解决这个问题,但我没有找到解决方案,所以我希望你能帮助我。我已经尝试从网站上提取数据。但问题是我不知道如何拆分列表以便 500g 转换为 500,g。问题是在网站上有时数量是 1,有时是 1 1/2 kg 或 sth。现在我需要将其转换为 CSV 文件,然后再转换为 MySQL 数据库。最后我想要的是一个 CSV 文件,其中包含以下列:成分 ID、成分、数量和成分的数量单位。例如: 0,肉,500,克。这是我已经从this 网站提取数据的代码:
import re
from bs4 import BeautifulSoup
import requests
import csv
urls_recipes = ['https://www.chefkoch.de/rezepte/3039711456645264/Ossobuco-a-la-Milanese.html']
mainurl = "https://www.chefkoch.de/rs/s0e1n1z1b0i1d1,2,3/Rezepte.html"
urls_urls = []
urls_recipes = ['https://www.chefkoch.de/rezepte/3039711456645264/Ossobuco-a-la-Milanese.html']
ingredients = []
menge = []
def read_recipes():
for url, id2 in zip(urls_recipes, range(len(urls_recipes))):
soup2 = BeautifulSoup(requests.get(url).content, "lxml")
for ingredient in soup2.select('.td-left'):
menge.append([*[re.sub(r'\s{2,}', ' ', ingredient.get_text(strip=True))]])
for ingredient in soup2.select('.recipe-ingredients h3, .td-right'):
if ingredient.name == 'h3':
ingredients.append([id2, *[ingredient.get_text(strip=True)]])
else:
ingredients.append([id2, *[re.sub(r'\s{2,}', ' ', ingredient.get_text(strip=True))]])
read_recipes()
希望你能帮助我,谢谢!
【问题讨论】:
标签: python mysql csv beautifulsoup export-to-csv