Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python拆分字符串并将其转换为一个注意到空字段的列表_Python_Mysql_Csv_Beautifulsoup_Export To Csv - Fatal编程技术网

Python拆分字符串并将其转换为一个注意到空字段的列表

Python拆分字符串并将其转换为一个注意到空字段的列表,python,mysql,csv,beautifulsoup,export-to-csv,Python,Mysql,Csv,Beautifulsoup,Export To Csv,我花了一整天的时间试图解决这个问题,但我没有找到解决办法,所以我希望你能帮助我。我已经试着从网站上提取数据了。但问题是,我不知道如何拆分列表,使500g转换为500g。问题是,在网站上,有时数量是1,有时是1 1/2公斤或其他。现在我需要将其转换为CSV文件,然后转换为MySQL数据库。最后我想要的是一个CSV文件,包含以下列:配料ID、配料、数量和配料的数量单位。例如: 0,肉,500克。这是我已经从网站中提取数据的代码: 我希望你能帮助我谢谢你 似乎包含分数的字符串使用unicode符号表示

我花了一整天的时间试图解决这个问题,但我没有找到解决办法,所以我希望你能帮助我。我已经试着从网站上提取数据了。但问题是,我不知道如何拆分列表,使500g转换为500g。问题是,在网站上,有时数量是1,有时是1 1/2公斤或其他。现在我需要将其转换为CSV文件,然后转换为MySQL数据库。最后我想要的是一个CSV文件,包含以下列:配料ID、配料、数量和配料的数量单位。例如: 0,肉,500克。这是我已经从网站中提取数据的代码:


我希望你能帮助我谢谢你

似乎包含分数的字符串使用unicode符号表示1/2等,因此我认为一个好的开始方法是查找特定的字符串并将其传递给str.replace。在本例中,拆分单位和数量很容易,因为它们之间用空格分隔。但如果遇到其他组合,可能有必要对其进行更多的概括。 以下代码适用于此特定示例:

进口稀土 从bs4导入BeautifulSoup 导入请求 导入csv 作为pd进口熊猫 URL_配方=['https://www.chefkoch.de/rezepte/3039711456645264/Ossobuco-a-la-Milanese.html'] mainurl=https://www.chefkoch.de/rs/s0e1n1z1b0i1d1,2,3/Rezepte.html URL\u URL=[] URL_配方=['https://www.chefkoch.de/rezepte/3039711456645264/Ossobuco-a-la-Milanese.html'] 成分=[] menge=[] einheit=[] 对于url,zipurls_菜谱中的id2,rangelenurls_菜谱: soup2=BeautifulSouprequests.geturl.content 对于soup2中的成分,选择“.td left”: 去掉多个空格并替换1/2 unicode字符 raw_string=re.subr'\s{2,}','',component.get_textstrip=True.replaceu'\u00BD',0.5 分为单位和数字 splitlist=raw_string.split menge.appendsplitlist[0] 如果lensplitlist==2: einheit.appendsplitlist[1] 其他: 艾因海特 对于soup2中的配料,选择“.配方配料h3,.td right”: 如果component.name==“h3”: 持续 其他: append[id2,re.subr'\s{2,}','',component.get_textstrip=True] 结果=pd.DataFrameComponents,列=[ID,Components] result.loc[:,unit]=einheit result.loc[:,amount]=menge 输出:

>>>结果 ID成分单位数量 0 0贝恩斯凯本,沃姆林德,约4厘米迪克格什恩。。。4. 100百万埃瓦 2 0 Zwiebeln 1 3.0克诺布劳什亨2 40卡洛顿1 5 0劳克斯坦根1 6 0斯塔德森勒里0.5 7 0托马滕,geschält剂量1 8 0托马滕马克EL 1 9 0罗特温祖姆阿卜勒申酒店 10 0弗雷什布吕赫牛场0.5升 11 0奥利文·祖姆·布拉顿 12 0 Gewürznelken 2 13 0皮门特克纳10 14 0 Wacholderbeeren 5 15 0普费弗克纳 16 0萨尔茨 17 0普费弗,施瓦兹,澳大利亚穆勒 180胸腺嘧啶 19 0迷迭香 20 0齐特罗宁,未贝汉德勒1 21 0克诺布劳什亨2 22 0布拉特佩特西里外滩1
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()