在其自己的单元格中将每个列表和字符串写入一行

时间:2017-11-25 17:52:55

标签: python python-3.x csv beautifulsoup

我提取了四个项目(一个字符串列表和三个字符串),我想在一行中写下所有四个项目。我认为这是可能的,只需要一些帮助。

r_ingredients_wtht_tags = []
    for link in r_links:
        r = requests.get(link)
        # print(r.status_code)
        if r.status_code == requests.codes.ok:
            soup = BeautifulSoup(r.content, "html.parser")
            r_name = soup.find('h1', {'itemprop': 'name'}).text.replace('recipe', '')
            r_prep_time = soup.select('li.recipe-meta-tag')[1].text
            r_ingrdnts_with_tags = soup.select('span[itemprop="ingredients"]')

            for r_ingrdnts in r_ingrdnts_with_tags:  # Remove span tags from list items
                r_ingredients_wtht_tags.append(r_ingrdnts.text)
            # print(r_ingredients_wtht_tags)
            # exit()
            r_image_src = soup.find('img', {'itemprop': 'image'}).get('src')
            r_image_url = 'https://website.com' + r_image_src # dummy website
            r_url = link

            # Download the recipe image
            print('Downloading image %s...' % (r_image_url))
            rec_image = requests.get(r_image_url)
            rec_image.raise_for_status()  # Will raise an exception if above request failed.

            # Create image folder to store current recipe image
            os.makedirs('recipe' + str(image_fold_count), exist_ok=True)

            # Save recipe image
            imageFile = open(os.path.join('recipe' + str(image_fold_count), os.path.basename(r_image_url)), 'wb')
            for chunk in rec_image.iter_content(100000):
                imageFile.write(chunk)
            imageFile.close()

            # write to csv file. NOTE TO SELF: MOVE THIS TO ITS OWN FUNCTION
            fileWriter.writerow(zip(r_name, r_prep_time, r_ingredients_wtht_tags, r_url +"\n"))

            image_fold_count += 1  # Increment recipe folder counter

1 个答案:

答案 0 :(得分:1)

如果r_ingredients_wtht_tags是一个列表,并且您希望所有四个元素在一行中但在单独的单元格中,那么您可以通过这种方式创建列表

row = [r_name, r_prep_time, r_ingredients_wtht_tags, r_url]

或将r_ingredients_wtht_tags转换为带有一些分隔符的字符串 您可以使用事件,csv会自动将其放入" "

ingredients = ",".join(r_ingredients_wtht_tags)
row = [r_name, r_prep_time, ingredients, r_url]

如果每种成分必须在分离的细胞中,你可以做

row = [r_name, r_prep_time] + r_ingredients_wtht_tags + [r_url]

并在不csv zip()的情况下将其写在"\n"中(没有csv - fileWriter.writerow(row) 会在行尾添加它)

this