如何在python中以HTML格式打印函数变量?

如何在python中以HTML格式打印函数变量?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我是一个自学成才的初学者,搜索了很多,但可能缺少搜索。我正在从两个网站抓取一些值,我想将它们与HTML输出进行比较。每一个网页,我都会把两个班级合并成一个列表。但当使用HTML进行输出时,我不希望打印所有列表。所以我做了一个函数来选择任何要打印的关键字。当我想打印出那个函数时,它在HTML输出时显示为“无”,但在控制台上显示了我想要的结果。那么如何显示这个特别列表呢 OS=Windows,Python3 from bs4 import BeautifulSoup import requests

我是一个自学成才的初学者,搜索了很多,但可能缺少搜索。我正在从两个网站抓取一些值,我想将它们与HTML输出进行比较。每一个网页,我都会把两个班级合并成一个列表。但当使用HTML进行输出时,我不希望打印所有列表。所以我做了一个函数来选择任何要打印的关键字。当我想打印出那个函数时,它在HTML输出时显示为“无”,但在控制台上显示了我想要的结果。那么如何显示这个特别列表呢

OS=Windows,Python3

from bs4 import BeautifulSoup
import requests
import datetime
import os
import webbrowser

carf_meySayf = requests.get('https://www.carrefoursa.com/tr/tr/meyve/c/1015?show=All').text
carf_soup = BeautifulSoup(carf_meySayf, 'lxml')

#spans
carf_name_span = carf_soup.find_all('span', {'class' : 'item-name'})
carf_price_span = carf_soup.find_all('span', {'class' : 'item-price'})


#spans to list
carf_name_list = [span.get_text() for span in carf_name_span]
carf_price_list = [span.get_text() for span in carf_price_span]

#combine lists
carf_mey_all = [carf_name_list +' = ' + carf_price_list for carf_name_list, carf_price_list in zip(carf_name_list, carf_price_list)]



#Function to choose and print special product

def test(namelist,product):
    for i in namelist:
        if product in i:
            print(i)


a = test(carf_mey_all,'Muz')


# Date
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# HTML part
html_str = """
<html>
    <title>Listeler</title>
        <h2>Tarih: %s</h2>
        <h3>Product & Shop List</h3>
            <table style="width:100%%">
                <tr>
                    <th>Carrefour</th>
                </tr>
                <tr>
                 %s
                </tr>
</html>
""" 

whole = html_str %(date,a)


Html_file= open("Meyve.html","w")
Html_file.write(whole)
Html_file.close()
从bs4导入美化组
导入请求
导入日期时间
导入操作系统
导入网络浏览器
carf_meySayf=requests.get('https://www.carrefoursa.com/tr/tr/meyve/c/1015?show=All)。文本
carf_汤=BeautifulSoup(carf_meySayf,'lxml')
#跨度
carf_name_span=carf_soup.find_all('span',{'class':'item name'})
carf_price_span=carf_soup.find_all('span',{'class':'item price'})
#要列出的范围
carf_name_list=[span.get_text()表示carf_name_span中的span]
carf_price_list=[span.get_text()表示carf_price_span中的span]
#合并列表
carf_mey_all=[carf_name_list+'='+carf_price_list for carf_name_list,carf_price_list in zip(carf_name_list,carf_price_list)]
#选择和打印特殊产品的功能
def测试(名称列表、产品):
对于姓名列表中的i:
如果产品在i中:
印刷品(一)
a=测试(全部为“Muz”)
#日期
date=datetime.datetime.now().strftime(“%Y-%m-%d%H:%m:%S”)
#HTML部分
html_str=”“”
李斯特
塔里赫:%s
产品及店铺清单
家乐福
%
""" 
整体=html_str%(日期,a)
Html_file=open(“Meyve.Html”、“w”)
Html_file.write(全部)
Html_file.close()

问题在于您的
test()
函数没有显式返回任何内容,因此它隐式返回
None

为了解决这个问题,
test()
应该累积它想要返回的文本(即,通过构建一个列表或字符串),并返回一个包含要插入
html\u str

的文本的字符串,例如,方法
test()
必须具有
返回值

def test(namelist,product):
    results = ''
    for i in namelist:
        if product in i:
            print(i)
            results += '<td>%s</td>\n' % i
    return results

注意:要成为有效的html,您需要添加

,非常感谢您的快速响应。不客气。如果有答案解决了你的问题,请将其标记为正确
<html>
<title>Listeler</title>
<h2>Tarih: 2018-12-29 07:34:00</h2>
<h3>Product & Shop List</h3>
<table style="width:100%">
  <tr>
    <th>Carrefour</th>
  </tr>
  <tr>
    <td>Muz = 6,99 TL</td>
    <td>İthal Muz = 12,90 TL</td>
    <td>Paket Yerli Muz = 9,99 TL</td>
  </tr>

</html>