我是一個自學者和初學者,搜索了很多,但可能缺乏搜索。我正在從兩個網站上抓取一些值,我想將它們與 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
提交
取消