我是一个自学者和初学者,搜索了很多,但可能缺乏搜索。我正在从两个网站上抓取一些值,我想将它们与 HTML 输出进行比较。每个网页,我都将两个类组合在一起并进入一个列表。但是当使用 HTML 进行输出时,我不希望打印所有列表。所以我做了功能来选择任何要打印的关键字。当我想打印出该函数时,它在 HTML 输出中显示为“无”,但在控制台上却变成了我想要的。那么如何显示那个特殊列表呢?操作系统= Windows ,Python3。from bs4 import BeautifulSoupimport requestsimport datetimeimport osimport webbrowsercarf_meySayf = requests.get('https://www.carrefoursa.com/tr/tr/meyve/c/1015?show=All').textcarf_soup = BeautifulSoup(carf_meySayf, 'lxml')#spanscarf_name_span = carf_soup.find_all('span', {'class' : 'item-name'})carf_price_span = carf_soup.find_all('span', {'class' : 'item-price'})#spans to listcarf_name_list = [span.get_text() for span in carf_name_span]carf_price_list = [span.get_text() for span in carf_price_span]#combine listscarf_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 productdef test(namelist,product): for i in namelist: if product in i: print(i)a = test(carf_mey_all,'Muz')# Datedate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")# HTML parthtml_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()
添加回答
举报
0/150
提交
取消