簡體   English   中英

我如何將所有循環變量存儲在 python 的字典中以在 django 中使用

[英]how do i store all loop variables in dictionary of python to use in django

PYTHON

import requests
import re
import os
import csv
from bs4 import BeautifulSoup

headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
}
searche=['note 8 pro','note 9 pro','boat','realme','poco']
for d in searche:
    truelink = d.replace(" ","-")
    truelinkk=('https://www.olx.in/hyderabad_g4058526/q-'+truelink+'?isSearchCall=true')
    r = requests.get(truelinkk,headers=headers).text
    soup=BeautifulSoup(r,'lxml')
    mobile=soup.find_all('li',class_='EIR5N')
    for  note9maxpro in mobile:
        mobi= note9maxpro.find('span',class_='_2tW1I').text
        price= note9maxpro.find('span',class_='_89yzn').text
        date= note9maxpro.find('span',class_='zLvFQ').text
        link=note9maxpro.a.get('href')
        orglink='https://www.olx.in'+link
        price = price.replace("₹ ","")

如何將所有循環變量存儲在 python 的字典中以便稍后在 django 中使用我想在 django 中使用此數據作為。 請給我解決方案以減少我的代碼行提前謝謝

for i in data:
 i.mobi
 i.price
 i.date

只是 append 他們給一個列表?

for d in searche:
    # (your code here)    

    data = list()
    
    for note9maxpro in mobile:
        element = dict()
        element["mobi"] = note9maxpro.find('span',class_='_2tW1I').text
        element["price"] = note9maxpro.find('span',class_='_89yzn').text
        # and so on
        data.append(element)
Context = {}
 i = 0

 for item in data:
    Context[f”item{i}”] = item.attribute
    i++

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM